""" Tests for initialization API smoke tests. Verifies: - or1-emu.AC5.1: IRAM initialization — PE has expected instructions at expected offsets - or1-emu.AC5.2: SM cell initialization — SM cells match config - or1-emu.AC5.3: Token injection — inject() routes tokens to correct stores by type """ import simpy from cm_inst import OutputStyle, ArithOp, MemOp, Port, RoutingOp, Instruction from emu import PEConfig, SMConfig, build_topology from sm_mod import Presence from tokens import CMToken, DyadToken, PELocalWriteToken, MonadToken, SMToken class TestAC51IRAMInitialization: """Test AC5.1: IRAM initialization""" def test_iram_contains_instructions_at_configured_offsets(self): """IRAM contains Instruction at offsets specified in PEConfig.""" env = simpy.Environment() pe_iram = { 0: Instruction( opcode=ArithOp.ADD, output=OutputStyle.INHERIT, has_const=False, dest_count=0, wide=False, fref=0, ), 5: Instruction( opcode=RoutingOp.CONST, output=OutputStyle.INHERIT, has_const=True, dest_count=0, wide=False, fref=0, ), } sys = build_topology( env, [PEConfig(pe_id=0, iram=pe_iram)], [], ) # Verify instructions are at expected offsets assert 0 in sys.pes[0].iram assert sys.pes[0].iram[0].opcode == ArithOp.ADD assert 5 in sys.pes[0].iram assert sys.pes[0].iram[5].opcode == RoutingOp.CONST def test_iram_does_not_contain_uninitialized_offsets(self): """IRAM does not contain offsets not specified in config.""" env = simpy.Environment() pe_iram = { 0: Instruction(opcode=ArithOp.ADD, output=OutputStyle.INHERIT, has_const=False, dest_count=0, wide=False, fref=0), 5: Instruction(opcode=RoutingOp.CONST, output=OutputStyle.INHERIT, has_const=True, dest_count=0, wide=False, fref=0), } sys = build_topology( env, [PEConfig(pe_id=0, iram=pe_iram)], [], ) # Verify uninitialized offsets are NOT in IRAM assert 3 not in sys.pes[0].iram assert 10 not in sys.pes[0].iram class TestAC52SMCellInitialization: """Test AC5.2: SM cell initialization""" def test_sm_cells_initialized_with_presence_and_data(self): """SM cells initialized via config contain expected presence and data.""" env = simpy.Environment() sm_config = SMConfig( sm_id=0, cell_count=512, initial_cells={ 0: (Presence.FULL, 42), 10: (Presence.RESERVED, None), }, ) sys = build_topology( env, [], [sm_config], ) # Verify cell 0: FULL with data 42 assert sys.sms[0].cells[0].pres == Presence.FULL assert sys.sms[0].cells[0].data_l == 42 # Verify cell 10: RESERVED with no data assert sys.sms[0].cells[10].pres == Presence.RESERVED assert sys.sms[0].cells[10].data_l is None def test_uninitialized_sm_cells_are_empty(self): """SM cells not in initial_cells config are EMPTY.""" env = simpy.Environment() sm_config = SMConfig( sm_id=0, cell_count=512, initial_cells={ 0: (Presence.FULL, 42), }, ) sys = build_topology( env, [], [sm_config], ) # Verify uninitialized cells are EMPTY assert sys.sms[0].cells[1].pres == Presence.EMPTY assert sys.sms[0].cells[1].data_l is None assert sys.sms[0].cells[100].pres == Presence.EMPTY assert sys.sms[0].cells[100].data_l is None class TestAC53TokenInjection: """Test AC5.3: Token injection API""" def test_inject_monad_token_to_pe(self): """inject() delivers MonadToken to correct PE's input_store.""" env = simpy.Environment() sys = build_topology( env, [PEConfig(pe_id=0, iram={}), PEConfig(pe_id=1, iram={})], [], ) # Create and inject token to PE0 token = MonadToken( target=0, offset=0, act_id=0, data=42, inline=False, ) sys.inject(token) # Verify token is in PE0's input_store assert len(sys.pes[0].input_store.items) == 1 assert sys.pes[0].input_store.items[0] == token def test_inject_multiple_tokens_to_correct_pes(self): """inject() places tokens in correct PE based on target.""" env = simpy.Environment() sys = build_topology( env, [PEConfig(pe_id=0, iram={}), PEConfig(pe_id=1, iram={})], [], ) # Inject token to PE0 token0 = MonadToken(target=0, offset=0, act_id=0, data=10, inline=False) sys.inject(token0) # Inject token to PE1 token1 = MonadToken(target=1, offset=0, act_id=0, data=20, inline=False) sys.inject(token1) # Verify tokens arrived at correct PEs assert len(sys.pes[0].input_store.items) == 1 assert sys.pes[0].input_store.items[0].data == 10 assert len(sys.pes[1].input_store.items) == 1 assert sys.pes[1].input_store.items[0].data == 20 def test_inject_sm_token_to_sm(self): """inject() delivers SMToken to correct SM's input_store.""" env = simpy.Environment() sys = build_topology( env, [PEConfig(pe_id=0, iram={})], [SMConfig(sm_id=0, cell_count=512), SMConfig(sm_id=1, cell_count=512)], ) # Create and inject token to SM0 token = SMToken( target=0, addr=5, op=MemOp.READ, flags=None, data=None, ret=CMToken(target=0, offset=0, act_id=0, data=0), ) sys.inject(token) # Verify token is in SM0's input_store assert len(sys.sms[0].input_store.items) == 1 assert sys.sms[0].input_store.items[0] == token def test_inject_sm_multiple_tokens_to_correct_sms(self): """inject() routes SMTokens to correct SM based on token.target.""" env = simpy.Environment() sys = build_topology( env, [PEConfig(pe_id=0, iram={})], [SMConfig(sm_id=0, cell_count=512), SMConfig(sm_id=1, cell_count=512)], ) # Inject token to SM0 token0 = SMToken( target=0, addr=10, op=MemOp.WRITE, flags=None, data=42, ret=None, ) sys.inject(token0) # Inject token to SM1 token1 = SMToken( target=1, addr=20, op=MemOp.READ, flags=None, data=None, ret=CMToken(target=0, offset=0, act_id=0, data=0), ) sys.inject(token1) # Verify tokens arrived at correct SMs assert len(sys.sms[0].input_store.items) == 1 assert sys.sms[0].input_store.items[0].addr == 10 assert len(sys.sms[1].input_store.items) == 1 assert sys.sms[1].input_store.items[0].addr == 20