multiplier: Generalize interface to the multiplier
[microwatt.git] / common.vhdl
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4
5 library work;
6 use work.decode_types.all;
7
8 package common is
9 -- Processor Version Number
10 constant PVR_MICROWATT : std_ulogic_vector(31 downto 0) := x"00630000";
11
12 -- MSR bit numbers
13 constant MSR_SF : integer := (63 - 0); -- Sixty-Four bit mode
14 constant MSR_EE : integer := (63 - 48); -- External interrupt Enable
15 constant MSR_PR : integer := (63 - 49); -- PRoblem state
16 constant MSR_IR : integer := (63 - 58); -- Instruction Relocation
17 constant MSR_DR : integer := (63 - 59); -- Data Relocation
18 constant MSR_RI : integer := (63 - 62); -- Recoverable Interrupt
19 constant MSR_LE : integer := (63 - 63); -- Little Endian
20
21 -- SPR numbers
22 subtype spr_num_t is integer range 0 to 1023;
23
24 function decode_spr_num(insn: std_ulogic_vector(31 downto 0)) return spr_num_t;
25
26 constant SPR_XER : spr_num_t := 1;
27 constant SPR_LR : spr_num_t := 8;
28 constant SPR_CTR : spr_num_t := 9;
29 constant SPR_DSISR : spr_num_t := 18;
30 constant SPR_DAR : spr_num_t := 19;
31 constant SPR_TB : spr_num_t := 268;
32 constant SPR_TBU : spr_num_t := 269;
33 constant SPR_DEC : spr_num_t := 22;
34 constant SPR_SRR0 : spr_num_t := 26;
35 constant SPR_SRR1 : spr_num_t := 27;
36 constant SPR_CFAR : spr_num_t := 28;
37 constant SPR_HSRR0 : spr_num_t := 314;
38 constant SPR_HSRR1 : spr_num_t := 315;
39 constant SPR_SPRG0 : spr_num_t := 272;
40 constant SPR_SPRG1 : spr_num_t := 273;
41 constant SPR_SPRG2 : spr_num_t := 274;
42 constant SPR_SPRG3 : spr_num_t := 275;
43 constant SPR_SPRG3U : spr_num_t := 259;
44 constant SPR_HSPRG0 : spr_num_t := 304;
45 constant SPR_HSPRG1 : spr_num_t := 305;
46 constant SPR_PID : spr_num_t := 48;
47 constant SPR_PRTBL : spr_num_t := 720;
48 constant SPR_PVR : spr_num_t := 287;
49
50 -- GPR indices in the register file (GPR only)
51 subtype gpr_index_t is std_ulogic_vector(4 downto 0);
52
53 -- Extended GPR indice (can hold an SPR)
54 subtype gspr_index_t is std_ulogic_vector(5 downto 0);
55
56 -- Some SPRs are stored in the register file, they use the magic
57 -- GPR numbers above 31.
58 --
59 -- The function fast_spr_num() returns the corresponding fast
60 -- pseudo-GPR number for a given SPR number. The result MSB
61 -- indicates if this is indeed a fast SPR. If clear, then
62 -- the SPR is not stored in the GPR file.
63 --
64 function fast_spr_num(spr: spr_num_t) return gspr_index_t;
65
66 -- Indices conversion functions
67 function gspr_to_gpr(i: gspr_index_t) return gpr_index_t;
68 function gpr_to_gspr(i: gpr_index_t) return gspr_index_t;
69 function gpr_or_spr_to_gspr(g: gpr_index_t; s: gspr_index_t) return gspr_index_t;
70 function is_fast_spr(s: gspr_index_t) return std_ulogic;
71
72 -- The XER is split: the common bits (CA, OV, SO, OV32 and CA32) are
73 -- in the CR file as a kind of CR extension (with a separate write
74 -- control). The rest is stored as a fast SPR.
75 type xer_common_t is record
76 ca : std_ulogic;
77 ca32 : std_ulogic;
78 ov : std_ulogic;
79 ov32 : std_ulogic;
80 so : std_ulogic;
81 end record;
82 constant xerc_init : xer_common_t := (others => '0');
83
84 type irq_state_t is (WRITE_SRR0, WRITE_SRR1);
85
86 -- For now, fixed 16 sources, make this either a parametric
87 -- package of some sort or an unconstrainted array.
88 type ics_to_icp_t is record
89 -- Level interrupts only, ICS just keeps prsenting the
90 -- highest priority interrupt. Once handling edge, something
91 -- smarter involving handshake & reject support will be needed
92 src : std_ulogic_vector(3 downto 0);
93 pri : std_ulogic_vector(7 downto 0);
94 end record;
95
96 -- This needs to die...
97 type ctrl_t is record
98 tb: std_ulogic_vector(63 downto 0);
99 dec: std_ulogic_vector(63 downto 0);
100 msr: std_ulogic_vector(63 downto 0);
101 cfar: std_ulogic_vector(63 downto 0);
102 irq_state : irq_state_t;
103 srr1: std_ulogic_vector(63 downto 0);
104 end record;
105
106 type Fetch1ToIcacheType is record
107 req: std_ulogic;
108 virt_mode : std_ulogic;
109 priv_mode : std_ulogic;
110 stop_mark: std_ulogic;
111 sequential: std_ulogic;
112 nia: std_ulogic_vector(63 downto 0);
113 end record;
114
115 type IcacheToDecode1Type is record
116 valid: std_ulogic;
117 stop_mark: std_ulogic;
118 fetch_failed: std_ulogic;
119 nia: std_ulogic_vector(63 downto 0);
120 insn: std_ulogic_vector(31 downto 0);
121 end record;
122
123 type Decode1ToDecode2Type is record
124 valid: std_ulogic;
125 stop_mark : std_ulogic;
126 nia: std_ulogic_vector(63 downto 0);
127 insn: std_ulogic_vector(31 downto 0);
128 ispr1: gspr_index_t; -- (G)SPR used for branch condition (CTR) or mfspr
129 ispr2: gspr_index_t; -- (G)SPR used for branch target (CTR, LR, TAR)
130 decode: decode_rom_t;
131 br_pred: std_ulogic; -- Branch was predicted to be taken
132 end record;
133 constant Decode1ToDecode2Init : Decode1ToDecode2Type :=
134 (valid => '0', stop_mark => '0', nia => (others => '0'), insn => (others => '0'),
135 ispr1 => (others => '0'), ispr2 => (others => '0'), decode => decode_rom_init, br_pred => '0');
136
137 type Decode1ToFetch1Type is record
138 redirect : std_ulogic;
139 redirect_nia : std_ulogic_vector(63 downto 0);
140 end record;
141
142 type Decode2ToExecute1Type is record
143 valid: std_ulogic;
144 unit : unit_t;
145 insn_type: insn_type_t;
146 nia: std_ulogic_vector(63 downto 0);
147 write_reg: gspr_index_t;
148 read_reg1: gspr_index_t;
149 read_reg2: gspr_index_t;
150 read_data1: std_ulogic_vector(63 downto 0);
151 read_data2: std_ulogic_vector(63 downto 0);
152 read_data3: std_ulogic_vector(63 downto 0);
153 bypass_data1: std_ulogic;
154 bypass_data2: std_ulogic;
155 bypass_data3: std_ulogic;
156 cr: std_ulogic_vector(31 downto 0);
157 bypass_cr : std_ulogic;
158 xerc: xer_common_t;
159 lr: std_ulogic;
160 rc: std_ulogic;
161 oe: std_ulogic;
162 invert_a: std_ulogic;
163 invert_out: std_ulogic;
164 input_carry: carry_in_t;
165 output_carry: std_ulogic;
166 input_cr: std_ulogic;
167 output_cr: std_ulogic;
168 is_32bit: std_ulogic;
169 is_signed: std_ulogic;
170 insn: std_ulogic_vector(31 downto 0);
171 data_len: std_ulogic_vector(3 downto 0);
172 byte_reverse : std_ulogic;
173 sign_extend : std_ulogic; -- do we need to sign extend?
174 update : std_ulogic; -- is this an update instruction?
175 reserve : std_ulogic; -- set for larx/stcx
176 br_pred : std_ulogic;
177 end record;
178 constant Decode2ToExecute1Init : Decode2ToExecute1Type :=
179 (valid => '0', unit => NONE, insn_type => OP_ILLEGAL, bypass_data1 => '0', bypass_data2 => '0', bypass_data3 => '0',
180 bypass_cr => '0', lr => '0', rc => '0', oe => '0', invert_a => '0',
181 invert_out => '0', input_carry => ZERO, output_carry => '0', input_cr => '0', output_cr => '0',
182 is_32bit => '0', is_signed => '0', xerc => xerc_init, reserve => '0', br_pred => '0',
183 byte_reverse => '0', sign_extend => '0', update => '0', nia => (others => '0'), read_data1 => (others => '0'), read_data2 => (others => '0'), read_data3 => (others => '0'), cr => (others => '0'), insn => (others => '0'), data_len => (others => '0'), others => (others => '0'));
184
185 type MultiplyInputType is record
186 valid: std_ulogic;
187 data1: std_ulogic_vector(63 downto 0);
188 data2: std_ulogic_vector(63 downto 0);
189 addend: std_ulogic_vector(127 downto 0);
190 is_32bit: std_ulogic;
191 not_result: std_ulogic;
192 end record;
193 constant MultiplyInputInit : MultiplyInputType := (valid => '0',
194 is_32bit => '0', not_result => '0',
195 others => (others => '0'));
196
197 type MultiplyOutputType is record
198 valid: std_ulogic;
199 result: std_ulogic_vector(127 downto 0);
200 overflow : std_ulogic;
201 end record;
202 constant MultiplyOutputInit : MultiplyOutputType := (valid => '0', overflow => '0',
203 others => (others => '0'));
204
205 type Execute1ToDividerType is record
206 valid: std_ulogic;
207 dividend: std_ulogic_vector(63 downto 0);
208 divisor: std_ulogic_vector(63 downto 0);
209 is_signed: std_ulogic;
210 is_32bit: std_ulogic;
211 is_extended: std_ulogic;
212 is_modulus: std_ulogic;
213 neg_result: std_ulogic;
214 end record;
215 constant Execute1ToDividerInit: Execute1ToDividerType := (valid => '0', is_signed => '0', is_32bit => '0',
216 is_extended => '0', is_modulus => '0',
217 neg_result => '0', others => (others => '0'));
218
219 type Decode2ToRegisterFileType is record
220 read1_enable : std_ulogic;
221 read1_reg : gspr_index_t;
222 read2_enable : std_ulogic;
223 read2_reg : gspr_index_t;
224 read3_enable : std_ulogic;
225 read3_reg : gpr_index_t;
226 end record;
227
228 type RegisterFileToDecode2Type is record
229 read1_data : std_ulogic_vector(63 downto 0);
230 read2_data : std_ulogic_vector(63 downto 0);
231 read3_data : std_ulogic_vector(63 downto 0);
232 end record;
233
234 type Decode2ToCrFileType is record
235 read : std_ulogic;
236 end record;
237
238 type CrFileToDecode2Type is record
239 read_cr_data : std_ulogic_vector(31 downto 0);
240 read_xerc_data : xer_common_t;
241 end record;
242
243 type Execute1ToFetch1Type is record
244 redirect: std_ulogic;
245 virt_mode: std_ulogic;
246 priv_mode: std_ulogic;
247 redirect_nia: std_ulogic_vector(63 downto 0);
248 end record;
249 constant Execute1ToFetch1Init : Execute1ToFetch1Type := (redirect => '0', virt_mode => '0',
250 priv_mode => '0', others => (others => '0'));
251
252 type Execute1ToLoadstore1Type is record
253 valid : std_ulogic;
254 op : insn_type_t; -- what ld/st or m[tf]spr or TLB op to do
255 nia : std_ulogic_vector(63 downto 0);
256 insn : std_ulogic_vector(31 downto 0);
257 addr1 : std_ulogic_vector(63 downto 0);
258 addr2 : std_ulogic_vector(63 downto 0);
259 data : std_ulogic_vector(63 downto 0); -- data to write, unused for read
260 write_reg : gpr_index_t;
261 length : std_ulogic_vector(3 downto 0);
262 ci : std_ulogic; -- cache-inhibited load/store
263 byte_reverse : std_ulogic;
264 sign_extend : std_ulogic; -- do we need to sign extend?
265 update : std_ulogic; -- is this an update instruction?
266 update_reg : gpr_index_t; -- if so, the register to update
267 xerc : xer_common_t;
268 reserve : std_ulogic; -- set for larx/stcx.
269 rc : std_ulogic; -- set for stcx.
270 virt_mode : std_ulogic; -- do translation through TLB
271 priv_mode : std_ulogic; -- privileged mode (MSR[PR] = 0)
272 end record;
273 constant Execute1ToLoadstore1Init : Execute1ToLoadstore1Type := (valid => '0', op => OP_ILLEGAL, ci => '0', byte_reverse => '0',
274 sign_extend => '0', update => '0', xerc => xerc_init,
275 reserve => '0', rc => '0', virt_mode => '0', priv_mode => '0',
276 nia => (others => '0'), insn => (others => '0'),
277 addr1 => (others => '0'), addr2 => (others => '0'), data => (others => '0'), length => (others => '0'),
278 others => (others => '0'));
279
280 type Loadstore1ToExecute1Type is record
281 busy : std_ulogic;
282 exception : std_ulogic;
283 invalid : std_ulogic;
284 perm_error : std_ulogic;
285 rc_error : std_ulogic;
286 badtree : std_ulogic;
287 segment_fault : std_ulogic;
288 instr_fault : std_ulogic;
289 end record;
290
291 type Loadstore1ToDcacheType is record
292 valid : std_ulogic;
293 load : std_ulogic; -- is this a load
294 dcbz : std_ulogic;
295 nc : std_ulogic;
296 reserve : std_ulogic;
297 virt_mode : std_ulogic;
298 priv_mode : std_ulogic;
299 addr : std_ulogic_vector(63 downto 0);
300 data : std_ulogic_vector(63 downto 0);
301 byte_sel : std_ulogic_vector(7 downto 0);
302 end record;
303
304 type DcacheToLoadstore1Type is record
305 valid : std_ulogic;
306 data : std_ulogic_vector(63 downto 0);
307 store_done : std_ulogic;
308 error : std_ulogic;
309 cache_paradox : std_ulogic;
310 end record;
311
312 type Loadstore1ToMmuType is record
313 valid : std_ulogic;
314 tlbie : std_ulogic;
315 slbia : std_ulogic;
316 mtspr : std_ulogic;
317 iside : std_ulogic;
318 load : std_ulogic;
319 priv : std_ulogic;
320 sprn : std_ulogic_vector(9 downto 0);
321 addr : std_ulogic_vector(63 downto 0);
322 rs : std_ulogic_vector(63 downto 0);
323 end record;
324
325 type MmuToLoadstore1Type is record
326 done : std_ulogic;
327 err : std_ulogic;
328 invalid : std_ulogic;
329 badtree : std_ulogic;
330 segerr : std_ulogic;
331 perm_error : std_ulogic;
332 rc_error : std_ulogic;
333 sprval : std_ulogic_vector(63 downto 0);
334 end record;
335
336 type MmuToDcacheType is record
337 valid : std_ulogic;
338 tlbie : std_ulogic;
339 doall : std_ulogic;
340 tlbld : std_ulogic;
341 addr : std_ulogic_vector(63 downto 0);
342 pte : std_ulogic_vector(63 downto 0);
343 end record;
344
345 type DcacheToMmuType is record
346 stall : std_ulogic;
347 done : std_ulogic;
348 err : std_ulogic;
349 data : std_ulogic_vector(63 downto 0);
350 end record;
351
352 type MmuToIcacheType is record
353 tlbld : std_ulogic;
354 tlbie : std_ulogic;
355 doall : std_ulogic;
356 addr : std_ulogic_vector(63 downto 0);
357 pte : std_ulogic_vector(63 downto 0);
358 end record;
359
360 type Loadstore1ToWritebackType is record
361 valid : std_ulogic;
362 write_enable: std_ulogic;
363 write_reg : gpr_index_t;
364 write_data : std_ulogic_vector(63 downto 0);
365 xerc : xer_common_t;
366 rc : std_ulogic;
367 store_done : std_ulogic;
368 end record;
369 constant Loadstore1ToWritebackInit : Loadstore1ToWritebackType := (valid => '0', write_enable => '0', xerc => xerc_init,
370 rc => '0', store_done => '0', write_data => (others => '0'), others => (others => '0'));
371
372 type Execute1ToWritebackType is record
373 valid: std_ulogic;
374 rc : std_ulogic;
375 write_enable : std_ulogic;
376 write_reg: gspr_index_t;
377 write_data: std_ulogic_vector(63 downto 0);
378 write_cr_enable : std_ulogic;
379 write_cr_mask : std_ulogic_vector(7 downto 0);
380 write_cr_data : std_ulogic_vector(31 downto 0);
381 write_xerc_enable : std_ulogic;
382 xerc : xer_common_t;
383 exc_write_enable : std_ulogic;
384 exc_write_reg : gspr_index_t;
385 exc_write_data : std_ulogic_vector(63 downto 0);
386 end record;
387 constant Execute1ToWritebackInit : Execute1ToWritebackType := (valid => '0', rc => '0', write_enable => '0',
388 write_cr_enable => '0', exc_write_enable => '0',
389 write_xerc_enable => '0', xerc => xerc_init,
390 write_data => (others => '0'), write_cr_mask => (others => '0'),
391 write_cr_data => (others => '0'), write_reg => (others => '0'),
392 exc_write_reg => (others => '0'), exc_write_data => (others => '0'));
393
394 type DividerToExecute1Type is record
395 valid: std_ulogic;
396 write_reg_data: std_ulogic_vector(63 downto 0);
397 overflow : std_ulogic;
398 end record;
399 constant DividerToExecute1Init : DividerToExecute1Type := (valid => '0', overflow => '0',
400 others => (others => '0'));
401
402 type WritebackToRegisterFileType is record
403 write_reg : gspr_index_t;
404 write_data : std_ulogic_vector(63 downto 0);
405 write_enable : std_ulogic;
406 end record;
407 constant WritebackToRegisterFileInit : WritebackToRegisterFileType := (write_enable => '0', write_data => (others => '0'), others => (others => '0'));
408
409 type WritebackToCrFileType is record
410 write_cr_enable : std_ulogic;
411 write_cr_mask : std_ulogic_vector(7 downto 0);
412 write_cr_data : std_ulogic_vector(31 downto 0);
413 write_xerc_enable : std_ulogic;
414 write_xerc_data : xer_common_t;
415 end record;
416 constant WritebackToCrFileInit : WritebackToCrFileType := (write_cr_enable => '0', write_xerc_enable => '0',
417 write_xerc_data => xerc_init,
418 write_cr_mask => (others => '0'),
419 write_cr_data => (others => '0'));
420
421 end common;
422
423 package body common is
424 function decode_spr_num(insn: std_ulogic_vector(31 downto 0)) return spr_num_t is
425 begin
426 return to_integer(unsigned(insn(15 downto 11) & insn(20 downto 16)));
427 end;
428 function fast_spr_num(spr: spr_num_t) return gspr_index_t is
429 variable n : integer range 0 to 31;
430 -- tmp variable introduced as workaround for VCS compilation
431 -- simulation was failing with subtype constraint mismatch error
432 -- see GitHub PR #173
433 variable tmp : std_ulogic_vector(4 downto 0);
434 begin
435 case spr is
436 when SPR_LR =>
437 n := 0;
438 when SPR_CTR =>
439 n:= 1;
440 when SPR_SRR0 =>
441 n := 2;
442 when SPR_SRR1 =>
443 n := 3;
444 when SPR_HSRR0 =>
445 n := 4;
446 when SPR_HSRR1 =>
447 n := 5;
448 when SPR_SPRG0 =>
449 n := 6;
450 when SPR_SPRG1 =>
451 n := 7;
452 when SPR_SPRG2 =>
453 n := 8;
454 when SPR_SPRG3 | SPR_SPRG3U =>
455 n := 9;
456 when SPR_HSPRG0 =>
457 n := 10;
458 when SPR_HSPRG1 =>
459 n := 11;
460 when SPR_XER =>
461 n := 12;
462 when others =>
463 n := 0;
464 return "000000";
465 end case;
466 tmp := std_ulogic_vector(to_unsigned(n, 5));
467 return "1" & tmp;
468 end;
469
470 function gspr_to_gpr(i: gspr_index_t) return gpr_index_t is
471 begin
472 return i(4 downto 0);
473 end;
474
475 function gpr_to_gspr(i: gpr_index_t) return gspr_index_t is
476 begin
477 return "0" & i;
478 end;
479
480 function gpr_or_spr_to_gspr(g: gpr_index_t; s: gspr_index_t) return gspr_index_t is
481 begin
482 if s(5) = '1' then
483 return s;
484 else
485 return gpr_to_gspr(g);
486 end if;
487 end;
488
489 function is_fast_spr(s: gspr_index_t) return std_ulogic is
490 begin
491 return s(5);
492 end;
493 end common;