1 from nmigen
.compat
.sim
import run_simulation
2 from nmigen
.cli
import verilog
, rtlil
3 from nmigen
import Module
, Signal
, Mux
, Elaboratable
5 from nmutil
.latch
import SRLatch
, latchregister
7 """ Computation Unit (aka "ALU Manager").
9 This module runs a "revolving door" set of three latches, based on
13 where one of them cannot be set on any given cycle.
14 (Note however that opc_l has been inverted (and qn used), due to SRLatch
15 default reset state being "0" rather than "1")
17 * When issue is first raised, a busy signal is sent out.
18 The src1 and src2 registers and the operand can be latched in
21 * Read request is set, which is acknowledged through the Scoreboard
22 to the priority picker, which generates (one and only one) Go_Read
23 at a time. One of those will (eventually) be this Computation Unit.
25 * Once Go_Read is set, the src1/src2/operand latch door shuts (locking
26 src1/src2/operand in place), and the ALU is told to proceed.
28 * As this is currently a "demo" unit, a countdown timer is activated
29 to simulate an ALU "pipeline", which activates "write request release",
30 and the ALU's output is captured into a temporary register.
32 * Write request release will go through a similar process as Read request,
33 resulting (eventually) in Go_Write being asserted.
35 * When Go_Write is asserted, two things happen: (1) the data in the temp
36 register is placed combinatorially onto the output, and (2) the
37 req_l latch is cleared, busy is dropped, and the Comp Unit is back
38 through its revolving door to do another task.
42 * bits[0:2] are for the ALU, add=0, sub=1, shift=2, mul=3
43 * bit[2] are the immediate (bit[2]=1 == immediate mode)
46 class ComputationUnitNoDelay(Elaboratable
):
47 def __init__(self
, rwid
, opwid
, alu
):
52 self
.counter
= Signal(4)
53 self
.go_rd_i
= Signal(reset_less
=True) # go read in
54 self
.go_wr_i
= Signal(reset_less
=True) # go write in
55 self
.issue_i
= Signal(reset_less
=True) # fn issue in
56 self
.shadown_i
= Signal(reset
=1) # shadow function, defaults to ON
57 self
.go_die_i
= Signal() # go die (reset)
59 self
.oper_i
= Signal(opwid
, reset_less
=True) # opcode in
60 self
.imm_i
= Signal(rwid
, reset_less
=True) # immediate in
61 self
.src1_i
= Signal(rwid
, reset_less
=True) # oper1 in
62 self
.src2_i
= Signal(rwid
, reset_less
=True) # oper2 in
64 self
.busy_o
= Signal(reset_less
=True) # fn busy out
65 self
.data_o
= Signal(rwid
, reset_less
=True) # Dest out
66 self
.rd_rel_o
= Signal(reset_less
=True) # release src1/src2 request
67 self
.req_rel_o
= Signal(reset_less
=True) # release request out (valid_o)
69 def elaborate(self
, platform
):
71 m
.submodules
.alu
= self
.alu
72 m
.submodules
.src_l
= src_l
= SRLatch(sync
=False)
73 m
.submodules
.opc_l
= opc_l
= SRLatch(sync
=False)
74 m
.submodules
.req_l
= req_l
= SRLatch(sync
=False)
77 reset_w
= Signal(reset_less
=True)
78 reset_r
= Signal(reset_less
=True)
79 m
.d
.comb
+= reset_w
.eq(self
.go_wr_i | self
.go_die_i
)
80 m
.d
.comb
+= reset_r
.eq(self
.go_rd_i | self
.go_die_i
)
82 # This is fascinating and very important to observe that this
83 # is in effect a "3-way revolving door". At no time may all 3
84 # latches be set at the same time.
86 # opcode latch (not using go_rd_i) - inverted so that busy resets to 0
87 m
.d
.sync
+= opc_l
.s
.eq(self
.issue_i
) # XXX NOTE: INVERTED FROM book!
88 m
.d
.sync
+= opc_l
.r
.eq(reset_w
) # XXX NOTE: INVERTED FROM book!
90 # src operand latch (not using go_wr_i)
91 m
.d
.sync
+= src_l
.s
.eq(self
.issue_i
)
92 m
.d
.sync
+= src_l
.r
.eq(reset_r
)
94 # dest operand latch (not using issue_i)
95 m
.d
.sync
+= req_l
.s
.eq(self
.go_rd_i
)
96 m
.d
.sync
+= req_l
.r
.eq(reset_w
)
99 # create a latch/register for the operand
100 oper_r
= Signal(self
.opwid
+1, reset_less
=True) # opcode reg
101 latchregister(m
, self
.oper_i
, oper_r
, self
.issue_i
)
103 # and one for the output from the ALU
104 data_r
= Signal(self
.rwid
, reset_less
=True) # Dest register
105 latchregister(m
, self
.alu
.o
, data_r
, req_l
.q
)
107 # get the top 2 bits for the ALU
108 m
.d
.comb
+= self
.alu
.op
.eq(oper_r
[0:2])
110 # 3rd bit is whether this is an immediate or not
111 op_is_imm
= Signal(reset_less
=True)
112 m
.d
.comb
+= op_is_imm
.eq(oper_r
[2])
114 # select immediate if opcode says so. however also change the latch
115 # to trigger *from* the opcode latch instead.
116 src2_or_imm
= Signal(self
.rwid
, reset_less
=True)
117 src_sel
= Signal(reset_less
=True)
118 m
.d
.comb
+= src_sel
.eq(Mux(op_is_imm
, opc_l
.qn
, src_l
.q
))
119 m
.d
.comb
+= src2_or_imm
.eq(Mux(op_is_imm
, self
.imm_i
, self
.src2_i
))
121 # create a latch/register for src1/src2
122 latchregister(m
, self
.src1_i
, self
.alu
.a
, src_l
.q
)
123 latchregister(m
, src2_or_imm
, self
.alu
.b
, src_sel
)
129 # all request signals gated by busy_o. prevents picker problems
131 m
.d
.comb
+= busy_o
.eq(opc_l
.q
) # busy out
132 m
.d
.comb
+= self
.rd_rel_o
.eq(src_l
.q
& busy_o
) # src1/src2 req rel
134 # the counter is just for demo purposes, to get the ALUs of different
135 # types to take arbitrary completion times
137 m
.d
.sync
+= self
.counter
.eq(0)
138 with m
.If(req_l
.qn
& busy_o
& (self
.counter
== 0)):
139 with m
.If(self
.alu
.op
== 2): # MUL, to take 5 instructions
140 m
.d
.sync
+= self
.counter
.eq(5)
141 with m
.Elif(self
.alu
.op
== 3): # SHIFT to take 7
142 m
.d
.sync
+= self
.counter
.eq(7)
143 with m
.Elif(self
.alu
.op
>= 4): # Branches take 6 (to test shadow)
144 m
.d
.sync
+= self
.counter
.eq(6)
145 with m
.Else(): # ADD/SUB to take 2
146 m
.d
.sync
+= self
.counter
.eq(2)
147 with m
.If(self
.counter
> 1):
148 m
.d
.sync
+= self
.counter
.eq(self
.counter
- 1)
149 with m
.If(self
.counter
== 1):
150 # write req release out. waits until shadow is dropped.
151 m
.d
.comb
+= self
.req_rel_o
.eq(req_l
.q
& busy_o
& self
.shadown_i
)
153 with m
.If(self
.go_wr_i
):
154 m
.d
.comb
+= self
.data_o
.eq(data_r
)
177 def scoreboard_sim(dut
):
178 yield dut
.dest_i
.eq(1)
179 yield dut
.issue_i
.eq(1)
181 yield dut
.issue_i
.eq(0)
183 yield dut
.src1_i
.eq(1)
184 yield dut
.issue_i
.eq(1)
188 yield dut
.issue_i
.eq(0)
190 yield dut
.go_read_i
.eq(1)
192 yield dut
.go_read_i
.eq(0)
194 yield dut
.go_write_i
.eq(1)
196 yield dut
.go_write_i
.eq(0)
199 def test_scoreboard():
200 from alu_hier
import ALU
202 dut
= ComputationUnitNoDelay(16, 8, alu
)
203 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
204 with
open("test_compalu.il", "w") as f
:
207 run_simulation(dut
, scoreboard_sim(dut
), vcd_name
='test_compalu.vcd')
209 if __name__
== '__main__':