Merge branch 'master' of ssh://git.libre-riscv.org:922/soc
[soc.git] / src / soc / decoder / power_regspec_map.py
1 """regspec_decode
2
3 function for the relationship between regspecs and Decode2Execute1Type
4
5 see https://libre-soc.org/3d_gpu/architecture/regfile/ section on regspecs
6 """
7 from nmigen import Const
8 from soc.regfile.regfiles import XERRegs, FastRegs
9 from soc.decoder.power_enums import CryIn
10
11
12 def regspec_decode(e, regfile, name):
13 """regspec_decode
14
15 this function encodes the understanding (relationship) between
16 Regfiles, Computation Units, and the Power ISA Decoder (PowerDecoder2).
17
18 based on the regspec, which contains the register file name and register
19 name, return a tuple of:
20
21 * how the decoder should determine whether the Function Unit needs
22 a Regport or not
23 * which Regfile port should be read to get that data
24 * when it comes to writing: likewise, which Regfile port should be written
25
26 Note that some of the port numbering encoding is *unary*. in the case
27 of "Full Condition Register", it's a full 8-bit mask of read/write-enables.
28 This actually matches directly with the XFX field in MTCR, and at
29 some point that 8-bit mask from the instruction could actually be passed directly through to full_cr (TODO).
30
31 For the INT and CR numbering, these are expressed in binary in the
32 instruction (note however that XFX in MTCR is unary-masked!)
33
34 XER is implicitly-encoded based on whether the operation has carry or
35 overflow.
36
37 FAST regfile is, again, implicitly encoded, back in PowerDecode2, based
38 on the type of operation (see DecodeB for an example).
39
40 The SPR regfile on the other hand is *binary*-encoded, and, furthermore,
41 has to be "remapped".
42 """
43
44 if regfile == 'INT':
45 # Int register numbering is *unary* encoded
46 if name == 'ra': # RA
47 return e.read_reg1.ok, 1<<e.read_reg1.data, None
48 if name == 'rb': # RB
49 return e.read_reg2.ok, 1<<e.read_reg2.data, None
50 if name == 'rc': # RS
51 return e.read_reg3.ok, 1<<e.read_reg3.data, None
52 if name == 'o': # RT
53 return e.write_reg.ok, None, 1<<e.write_reg.data
54 if name == 'o1': # RA (update mode: LD/ST EA)
55 return e.write_ea.ok, None, 1<<e.write_ea.data
56
57 if regfile == 'CR':
58 # CRRegs register numbering is *unary* encoded
59 if name == 'full_cr': # full CR
60 return e.read_cr_whole, 0b11111111, 0b11111111
61 if name == 'cr_a': # CR A
62 return e.read_cr1.ok, 1<<e.read_cr1.data, 1<<e.write_cr.data
63 if name == 'cr_b': # CR B
64 return e.read_cr2.ok, 1<<e.read_cr2.data, None
65 if name == 'cr_c': # CR C
66 return e.read_cr3.ok, 1<<e.read_cr2.data, None
67
68 if regfile == 'XER':
69 # XERRegs register numbering is *unary* encoded
70 SO = 1<<XERRegs.SO
71 CA = 1<<XERRegs.CA
72 OV = 1<<XERRegs.OV
73 if name == 'xer_so':
74 return e.oe.oe[0] & e.oe.oe_ok, SO, SO
75 if name == 'xer_ov':
76 return e.oe.oe[0] & e.oe.oe_ok, OV, OV
77 if name == 'xer_ca':
78 return (e.input_carry == CryIn.CA.value), CA, CA
79
80 if regfile == 'FAST':
81 # FAST register numbering is *unary* encoded
82 PC = 1<<FastRegs.PC
83 MSR = 1<<FastRegs.MSR
84 CTR = 1<<FastRegs.CTR
85 LR = 1<<FastRegs.LR
86 TAR = 1<<FastRegs.TAR
87 SRR1 = 1<<FastRegs.SRR1
88 SRR2 = 1<<FastRegs.SRR2
89 if name in ['cia', 'nia']:
90 return Const(1), PC, PC # TODO: detect read-conditions
91 if name == 'msr':
92 return Const(1), MSR, MS # TODO: detect read-conditions
93 # TODO: remap the SPR numbers to FAST regs
94 if name == 'spr1':
95 return e.read_fast1.ok, 1<<e.read_fast1.data, 1<<e.write_fast1.data
96 if name == 'spr2':
97 return e.read_fast2.ok, 1<<e.read_fast2.data, 1<<e.write_fast2.data
98
99 if regfile == 'SPR':
100 assert False, "regfile TODO %s %s %d" % (refgile, repr(regspec), idx)
101 assert False, "regspec not found %s %s %d" % (refgile, repr(regspec), idx)