1597f36497e5fffebfe55a37755d75951c670c85
[soc.git] / src / scoreboard / ldst_matrix.py
1 """ Mitch Alsup 6600-style LD/ST Memory Scoreboard Matrix (sparse vector)
2
3 6600 LD/ST Dependency Table Matrix inputs / outputs
4 ---------------------------------------------------
5
6 Relevant comments (p45-46):
7
8 * If there are no WAR dependencies on a Load instruction with a computed
9 address it can assert Bank_Addressable and Translate_Addressable.
10
11 * If there are no RAW dependencies on a Store instruction with both a
12 write permission and store data present it can assert Bank_Addressable
13
14 Relevant bugreports:
15
16 * http://bugs.libre-riscv.org/show_bug.cgi?id=81
17
18 Notes:
19
20 * Load Hit (or Store Hit with Data) are asserted by the LD/ST Computation
21 Unit when it has data and address ready
22
23 * Asserting the ld_hit_i (or stwd_hit_i) *requires* that the output be
24 captured or at least taken into consideration for the next LD/STs
25 *right then*. Failure to observe the xx_hold_xx_o *will* result in
26 data corruption, as they are *only* asserted if xx_hit_i is asserted
27
28 * The hold signals still have to go through "maybe address clashes"
29 detection, they cannot just be used as-is to stop a LD/ST.
30
31 """
32
33 from nmigen.compat.sim import run_simulation
34 from nmigen.cli import verilog, rtlil
35 from nmigen import Module, Signal, Elaboratable, Array, Cat, Const
36
37 from ldst_dep_cell import LDSTDepCell
38
39
40 class LDSTDepMatrix(Elaboratable):
41 """ implements 11.4.12 mitch alsup LD/ST Dependency Matrix, p46
42 actually a sparse matrix along the diagonal.
43
44 load-hold-store and store-hold-load accumulate in a priority-picking
45 fashion, ORing together. the OR gate from the dependency cell is
46 here.
47 """
48 def __init__(self, n_ldst):
49 self.n_ldst = n_ldst # X and Y (FUs)
50 self.load_i = Signal(n_ldst, reset_less=True) # load pending in
51 self.stor_i = Signal(n_ldst, reset_less=True) # store pending in
52 self.issue_i = Signal(n_ldst, reset_less=True) # Issue in
53
54 self.load_hit_i = Signal(n_ldst, reset_less=True) # load hit in
55 self.stwd_hit_i = Signal(n_ldst, reset_less=True) # store w/data hit in
56
57 # outputs
58 self.ld_hold_st_o = Signal(n_ldst, reset_less=True) # load holds st out
59 self.st_hold_ld_o = Signal(n_ldst, reset_less=True) # st holds load out
60
61 def elaborate(self, platform):
62 m = Module()
63
64 # ---
65 # matrix of dependency cells. actually, LDSTDepCell is a row, now
66 # ---
67 dm = Array(LDSTDepCell(self.n_ldst) for f in range(self.n_ldst))
68 for fu in range(self.n_ldst):
69 setattr(m.submodules, "dm_fu%d" % (fu), dm[fu])
70
71 # ---
72 # connect Function Unit vector, all horizontal
73 # ---
74 lhs_l = []
75 shl_l = []
76 load_l = []
77 stor_l = []
78 issue_l = []
79 lh_l = []
80 sh_l = []
81 for fu in range(self.n_ldst):
82 dc = dm[fu]
83 # accumulate load-hold-store / store-hold-load bits (horizontal)
84 lhs_l.append(dc.ld_hold_st_o)
85 shl_l.append(dc.st_hold_ld_o)
86 # accumulate inputs (for Cat'ing later) - TODO: must be a better way
87 load_l.append(dc.load_h_i)
88 stor_l.append(dc.stor_h_i)
89 issue_l.append(dc.issue_i)
90
91 # load-hit and store-with-data-hit go in vertically (top)
92 m.d.comb += [dc.load_hit_i.eq(self.load_hit_i),
93 dc.stwd_hit_i.eq(self.stwd_hit_i)
94 ]
95
96 # connect cell inputs using Cat(*list_of_stuff)
97 m.d.comb += [Cat(*load_l).eq(self.load_i),
98 Cat(*stor_l).eq(self.stor_i),
99 Cat(*issue_l).eq(self.issue_i),
100 ]
101 # connect the load-hold-store / store-hold-load OR-accumulated outputs
102 m.d.comb += self.ld_hold_st_o.eq(Cat(*lhs_l))
103 m.d.comb += self.st_hold_ld_o.eq(Cat(*shl_l))
104
105 # the load/store input also needs to be connected to "top" (vertically)
106 for fu in range(self.n_ldst):
107 load_v_l = []
108 stor_v_l = []
109 for fux in range(self.n_ldst):
110 dc = dm[fux]
111 load_v_l.append(dc.load_v_i[fu])
112 stor_v_l.append(dc.stor_v_i[fu])
113 m.d.comb += [Cat(*load_v_l).eq(self.load_i),
114 Cat(*stor_v_l).eq(self.stor_i),
115 ]
116
117 return m
118
119 def __iter__(self):
120 yield self.load_i
121 yield self.stor_i
122 yield self.issue_i
123 yield self.load_hit_i
124 yield self.stwd_hit_i
125 yield self.ld_hold_st_o
126 yield self.st_hold_ld_o
127
128 def ports(self):
129 return list(self)
130
131 def d_matrix_sim(dut):
132 """ XXX TODO
133 """
134 yield dut.dest_i.eq(1)
135 yield dut.issue_i.eq(1)
136 yield
137 yield dut.issue_i.eq(0)
138 yield
139 yield dut.src1_i.eq(1)
140 yield dut.issue_i.eq(1)
141 yield
142 yield dut.issue_i.eq(0)
143 yield
144 yield dut.go_rd_i.eq(1)
145 yield
146 yield dut.go_rd_i.eq(0)
147 yield
148 yield dut.go_wr_i.eq(1)
149 yield
150 yield dut.go_wr_i.eq(0)
151 yield
152
153 def test_d_matrix():
154 dut = LDSTDepMatrix(n_ldst=4)
155 vl = rtlil.convert(dut, ports=dut.ports())
156 with open("test_ld_st_matrix.il", "w") as f:
157 f.write(vl)
158
159 run_simulation(dut, d_matrix_sim(dut), vcd_name='test_ld_st_matrix.vcd')
160
161 if __name__ == '__main__':
162 test_d_matrix()