1818project_root = os .path .dirname (script_dir )
1919sys .path .insert (0 , project_root )
2020
21- from src .nand_controller import NANDController
22- from src .utils .config import Config , load_config
21+ from src .opennandlab . simulator import NANDController
22+ from src .opennandlab .config import SimulatorConfig
2323
2424
25- def load_configuration ():
26- """Load configuration with fallback to default locations"""
27- # Look for config in standard locations
28- config_paths = [
29- os .path .join (project_root , 'resources' , 'config' , 'config.yaml' ),
30- os .path .join ('resources' , 'config' , 'config.yaml' ),
31- 'config.yaml'
32- ]
33-
34- for path in config_paths :
35- if os .path .exists (path ):
36- print (f"Loading configuration from { path } " )
37- return load_config (path )
38-
39- # Create minimal default configuration if no file found
40- print ("No configuration file found. Using default configuration." )
41- config_dict = {
42- 'nand_config' : {
43- 'page_size' : 4096 ,
44- 'block_size' : 64 ,
45- 'num_blocks' : 1024 ,
46- 'oob_size' : 128
47- },
48- 'simulation' : {
49- 'enabled' : True , # Use simulator
50- 'error_rate' : 0.0001 ,
51- 'initial_bad_block_rate' : 0.001
52- }
53- }
54- return Config (config_dict )
55-
5625def basic_operations_example ():
5726 """
5827 Demonstrate basic operations with NAND flash controller
5928 """
6029 print ("=== Basic NAND Flash Operations Example ===" )
6130
62- # Load configuration
63- config = load_configuration ()
31+ # Load default configuration
32+ config = SimulatorConfig ()
6433
6534 # Create NAND controller (simulation mode for safety)
66- config_dict = config .config if hasattr (config , 'config' ) else config
67- config_dict ['simulation' ] = {'enabled' : True }
68-
69- controller = NANDController (Config (config_dict ))
35+ controller = NANDController (config , simulation_mode = True )
7036 print ("NAND controller created" )
7137
7238 try :
@@ -82,35 +48,20 @@ def basic_operations_example():
8248 print (f"Block Size: { device_info ['config' ]['block_size' ]} pages" )
8349 print (f"Number of Blocks: { device_info ['config' ]['num_blocks' ]} " )
8450
85- # Find a good block for the demonstration
86- print ("\n --- Finding a Good Block ---" )
87- block = None
88- for b in range (10 , 20 ): # Try blocks 10-19 to avoid system blocks
89- if not controller .is_bad_block (b ):
90- block = b
91- print (f"Found good block: { block } " )
92- break
51+ # Writing and reading use logical block numbers (lbn) in v2.0.0
52+ # The FTL manages the physical translation.
53+ lbn = 10 # A random logical block number
9354
94- if block is None :
95- print ("Could not find a good block. Exiting." )
96- return
97-
98- # Erase the block first
99- print ("\n --- Erasing Block ---" )
100- controller .erase_block (block )
101- print (f"Block { block } erased successfully" )
102-
103- # Write to the first page
55+ # Write to the logical page
10456 print ("\n --- Writing Data ---" )
105- page = 0
106- test_data = f"Test data written to block { block } , page { page } at { time .time ()} " .encode ('utf-8' )
107- controller .write_page (block , page , test_data )
108- print (f"Data written to block { block } , page { page } " )
57+ test_data = f"Test data written to logical page { lbn } at { time .time ()} " .encode ('utf-8' )
58+ controller .write_page (lbn , test_data )
59+ print (f"Data written to logical page { lbn } " )
10960
110- # Read from the first page
61+ # Read from the logical page
11162 print ("\n --- Reading Data ---" )
112- read_data = controller .read_page (block , page )
113- print (f"Read { len (read_data )} bytes from block { block } , page { page } " )
63+ read_data = controller .read_page (lbn )
64+ print (f"Read { len (read_data )} bytes from logical page { lbn } " )
11465
11566 # Verify the data
11667 if test_data in read_data :
@@ -122,17 +73,12 @@ def basic_operations_example():
12273 print (f"Original: { test_data } " )
12374 print (f"Read: { read_data [:100 ]} " )
12475
125- # Demonstrate bad block handling
126- print ("\n --- Bad Block Handling ---" )
127- next_good = controller .get_next_good_block (block )
128- print (f"Next good block after { block } is { next_good } " )
129-
13076 # Demonstrate error handling
13177 print ("\n --- Error Handling Example ---" )
13278 try :
133- # Try to access an invalid block (beyond range)
134- invalid_block = controller .num_blocks + 10
135- controller .read_page (invalid_block , 0 )
79+ # Try to access an invalid logical page (beyond range)
80+ invalid_lbn = controller .ftl . num_logical_pages + 10
81+ controller .read_page (invalid_lbn )
13682 except Exception as e :
13783 print (f"Expected error caught: { e } " )
13884
0 commit comments