OR-1 dataflow CPU sketch
1"""
2Tests for initialization API smoke tests.
3
4Verifies:
5- or1-emu.AC5.1: IRAM initialization — PE has expected instructions at expected offsets
6- or1-emu.AC5.2: SM cell initialization — SM cells match config
7- or1-emu.AC5.3: Token injection — inject() routes tokens to correct stores by type
8"""
9
10import simpy
11
12from cm_inst import OutputStyle, ArithOp, MemOp, Port, RoutingOp, Instruction
13from emu import PEConfig, SMConfig, build_topology
14from sm_mod import Presence
15from tokens import CMToken, DyadToken, PELocalWriteToken, MonadToken, SMToken
16
17
18class TestAC51IRAMInitialization:
19 """Test AC5.1: IRAM initialization"""
20
21 def test_iram_contains_instructions_at_configured_offsets(self):
22 """IRAM contains Instruction at offsets specified in PEConfig."""
23 env = simpy.Environment()
24
25 pe_iram = {
26 0: Instruction(
27 opcode=ArithOp.ADD,
28 output=OutputStyle.INHERIT,
29 has_const=False,
30 dest_count=0,
31 wide=False,
32 fref=0,
33 ),
34 5: Instruction(
35 opcode=RoutingOp.CONST,
36 output=OutputStyle.INHERIT,
37 has_const=True,
38 dest_count=0,
39 wide=False,
40 fref=0,
41 ),
42 }
43
44 sys = build_topology(
45 env,
46 [PEConfig(pe_id=0, iram=pe_iram)],
47 [],
48 )
49
50 # Verify instructions are at expected offsets
51 assert 0 in sys.pes[0].iram
52 assert sys.pes[0].iram[0].opcode == ArithOp.ADD
53 assert 5 in sys.pes[0].iram
54 assert sys.pes[0].iram[5].opcode == RoutingOp.CONST
55
56 def test_iram_does_not_contain_uninitialized_offsets(self):
57 """IRAM does not contain offsets not specified in config."""
58 env = simpy.Environment()
59
60 pe_iram = {
61 0: Instruction(opcode=ArithOp.ADD, output=OutputStyle.INHERIT, has_const=False, dest_count=0, wide=False, fref=0),
62 5: Instruction(opcode=RoutingOp.CONST, output=OutputStyle.INHERIT, has_const=True, dest_count=0, wide=False, fref=0),
63 }
64
65 sys = build_topology(
66 env,
67 [PEConfig(pe_id=0, iram=pe_iram)],
68 [],
69 )
70
71 # Verify uninitialized offsets are NOT in IRAM
72 assert 3 not in sys.pes[0].iram
73 assert 10 not in sys.pes[0].iram
74
75
76class TestAC52SMCellInitialization:
77 """Test AC5.2: SM cell initialization"""
78
79 def test_sm_cells_initialized_with_presence_and_data(self):
80 """SM cells initialized via config contain expected presence and data."""
81 env = simpy.Environment()
82
83 sm_config = SMConfig(
84 sm_id=0,
85 cell_count=512,
86 initial_cells={
87 0: (Presence.FULL, 42),
88 10: (Presence.RESERVED, None),
89 },
90 )
91
92 sys = build_topology(
93 env,
94 [],
95 [sm_config],
96 )
97
98 # Verify cell 0: FULL with data 42
99 assert sys.sms[0].cells[0].pres == Presence.FULL
100 assert sys.sms[0].cells[0].data_l == 42
101
102 # Verify cell 10: RESERVED with no data
103 assert sys.sms[0].cells[10].pres == Presence.RESERVED
104 assert sys.sms[0].cells[10].data_l is None
105
106 def test_uninitialized_sm_cells_are_empty(self):
107 """SM cells not in initial_cells config are EMPTY."""
108 env = simpy.Environment()
109
110 sm_config = SMConfig(
111 sm_id=0,
112 cell_count=512,
113 initial_cells={
114 0: (Presence.FULL, 42),
115 },
116 )
117
118 sys = build_topology(
119 env,
120 [],
121 [sm_config],
122 )
123
124 # Verify uninitialized cells are EMPTY
125 assert sys.sms[0].cells[1].pres == Presence.EMPTY
126 assert sys.sms[0].cells[1].data_l is None
127 assert sys.sms[0].cells[100].pres == Presence.EMPTY
128 assert sys.sms[0].cells[100].data_l is None
129
130
131class TestAC53TokenInjection:
132 """Test AC5.3: Token injection API"""
133
134 def test_inject_monad_token_to_pe(self):
135 """inject() delivers MonadToken to correct PE's input_store."""
136 env = simpy.Environment()
137
138 sys = build_topology(
139 env,
140 [PEConfig(pe_id=0, iram={}), PEConfig(pe_id=1, iram={})],
141 [],
142 )
143
144 # Create and inject token to PE0
145 token = MonadToken(
146 target=0,
147 offset=0,
148 act_id=0,
149 data=42,
150 inline=False,
151 )
152
153 sys.inject(token)
154
155 # Verify token is in PE0's input_store
156 assert len(sys.pes[0].input_store.items) == 1
157 assert sys.pes[0].input_store.items[0] == token
158
159 def test_inject_multiple_tokens_to_correct_pes(self):
160 """inject() places tokens in correct PE based on target."""
161 env = simpy.Environment()
162
163 sys = build_topology(
164 env,
165 [PEConfig(pe_id=0, iram={}), PEConfig(pe_id=1, iram={})],
166 [],
167 )
168
169 # Inject token to PE0
170 token0 = MonadToken(target=0, offset=0, act_id=0, data=10, inline=False)
171 sys.inject(token0)
172
173 # Inject token to PE1
174 token1 = MonadToken(target=1, offset=0, act_id=0, data=20, inline=False)
175 sys.inject(token1)
176
177 # Verify tokens arrived at correct PEs
178 assert len(sys.pes[0].input_store.items) == 1
179 assert sys.pes[0].input_store.items[0].data == 10
180
181 assert len(sys.pes[1].input_store.items) == 1
182 assert sys.pes[1].input_store.items[0].data == 20
183
184 def test_inject_sm_token_to_sm(self):
185 """inject() delivers SMToken to correct SM's input_store."""
186 env = simpy.Environment()
187
188 sys = build_topology(
189 env,
190 [PEConfig(pe_id=0, iram={})],
191 [SMConfig(sm_id=0, cell_count=512), SMConfig(sm_id=1, cell_count=512)],
192 )
193
194 # Create and inject token to SM0
195 token = SMToken(
196 target=0,
197 addr=5,
198 op=MemOp.READ,
199 flags=None,
200 data=None,
201 ret=CMToken(target=0, offset=0, act_id=0, data=0),
202 )
203
204 sys.inject(token)
205
206 # Verify token is in SM0's input_store
207 assert len(sys.sms[0].input_store.items) == 1
208 assert sys.sms[0].input_store.items[0] == token
209
210 def test_inject_sm_multiple_tokens_to_correct_sms(self):
211 """inject() routes SMTokens to correct SM based on token.target."""
212 env = simpy.Environment()
213
214 sys = build_topology(
215 env,
216 [PEConfig(pe_id=0, iram={})],
217 [SMConfig(sm_id=0, cell_count=512), SMConfig(sm_id=1, cell_count=512)],
218 )
219
220 # Inject token to SM0
221 token0 = SMToken(
222 target=0,
223 addr=10,
224 op=MemOp.WRITE,
225 flags=None,
226 data=42,
227 ret=None,
228 )
229 sys.inject(token0)
230
231 # Inject token to SM1
232 token1 = SMToken(
233 target=1,
234 addr=20,
235 op=MemOp.READ,
236 flags=None,
237 data=None,
238 ret=CMToken(target=0, offset=0, act_id=0, data=0),
239 )
240 sys.inject(token1)
241
242 # Verify tokens arrived at correct SMs
243 assert len(sys.sms[0].input_store.items) == 1
244 assert sys.sms[0].input_store.items[0].addr == 10
245
246 assert len(sys.sms[1].input_store.items) == 1
247 assert sys.sms[1].input_store.items[0].addr == 20