Verilog 2 - Design Examples: 6.375 Complex Digital Systems Arvind February 9, 2009
Verilog 2 - Design Examples: 6.375 Complex Digital Systems Arvind February 9, 2009
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-2
Use continuous assignments (assign) Use always@(*) blocks with blocking assignments (=)
always @(*) begin out = 2d0; if (in1 == 1) out = 2d1; else if (in2 == 1) out = 2d2; end
Sequential logic:
February 9, 2009
Only leaf modules should have functionality; use higherlevel modules only for wiring together sub-modules
http://csg.csail.mit.edu/6.375/
Use only positive-edge triggered flip-flops for state Do not assign the same variable from more than one always block
L03-3
An example
wire A_in, B_in, C_in; reg A_out, B_out, C_out;
always @( posedge clk ) begin A_out <= A_in; B_out <= B_in; C_out <= C_in; end assign B_in = A_out + 1; assign C_in = B_out + 1;
+1
+1
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-4
+1
+1
Does it have the same functionality? Yes. But why? Need to understand something about Verilog execution semantics
L03-5
February 9, 2009
http://csg.csail.mit.edu/6.375/
+1
+1
+1
+1
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-6
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-7
A 1 B 2 C
C B A
On clock edge all those events which are sensitive to the clock are added to the active event queue in any order!
L03-8
February 9, 2009
http://csg.csail.mit.edu/6.375/
A 1 B 2 C
C C A 1 B B
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-9
A 1 B 2 C
Event queue is emptied B evaluates and as a consequence 2 to next before we go is added to the event queue clock cycle
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-10
Non-blocking assignment
Within a clock cycle all RHS variables are read first and all the LHS variables are updated together at the end of the clock cycle
Consequently, two event queues have to be maintained one keeps the computations to be performed while the other keeps the variables to be updated
February 9, 2009 http://csg.csail.mit.edu/6.375/ L03-11
A 1 B 2 C
2 1
C B A R R R
Non-Blocking Queue C B A L L L
February 9, 2009
http://csg.csail.mit.edu/6.375/
Variables in RHS of always blocks are not updated until all inputs (e.g. LHS + dependencies) are evaluated
L03-12
+1
+1
+1
+1
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-13
Data dependent for and while loops Additional behavioral datatypes: integer, real Magic initialization blocks: initial Magic delay statements: #<delay> System calls: $display, $assert, $finish
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-14
February 9, 2009
L03-15
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-16
GCD in C
int GCD( int inA, int inB) { int done = 0; int A = inA; Such a GCD description can be int B = inB; easily written in Behavioral while ( !done ) { if ( A < B ) Verilog { swap = A; A = B; It can be simulated but it will B = swap; have nothing to do with } hardware, i.e. it wont else if ( B != 0 ) synthesize. A = A - B; else done = 1; } return A; }
February 9, 2009 http://csg.csail.mit.edu/6.375/ L03-17
February 9, 2009
L03-18
February 9, 2009
L03-20
clk
reset
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-21
zero? A
lt sub
A = inA; B = inB;
February 9, 2009
http://csg.csail.mit.edu/6.375/
while ( !done ) begin if ( A < B ) swap = A; A = B; B = swap; else if (B != 0) A = A - B; else done = 1; End Y = A; L03-22
Step 3: Add the control unit Control unit to sequence the datapath should be
A A sel en B B sel en B=0 A<B
zero? A
lt
A = inA; B = inB;
sub
designed to be either busy or waiting for input or waiting for output to be picked up
February 9, 2009
http://csg.csail.mit.edu/6.375/
while ( !done ) begin if ( A < B ) swap = A; A = B; B = swap; else if (B != 0) A = A - B; else done = 1; End Y = A; L03-23
zero? A B
lt sub
// Control signals (ctrl->dpath) input A_en, input B_en, input [1:0] A_sel, input B_sel,
// Control signals (dpath->ctrl) output B_zero, output A_lt_B
February 9, 2009
);
http://csg.csail.mit.edu/6.375/
L03-24
zero? A
lt sub
wire [W-1:0] B_out; vcMux2#(W) B_mux ( .in0 (operand_B), .in1 (A), .sel (B_sel), .out (B_out) );
Using explicit state helps eliminate issues with non-blocking assignments Continuous assignment combinational logic is fine
vcEDFF_pf#(W) B_pf ( .clk (clk), .en_p (B_en), .d_p (B_out), .q_np (B) );
assign assign assign assign
http://csg.csail.mit.edu/6.375/
input_availble
CALC Swapping and subtracting (B=0) result_taken DONE Waiting for consumer to take the result
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-27
Localparams are not really parameters at all. They are scoped constants.
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-28
February 9, 2009
http://csg.csail.mit.edu/6.375/
result_taken
DONE
http://csg.csail.mit.edu/6.375/
L03-30
B=0
A<B
zero? A B
lt sub
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-31
Behavioral Model
RTL Model
Test Outputs
Test Outputs
Identical?
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-32
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-33
SMIPSv2
SMIPSv3
February 9, 2009
SMIPSv1 ISA
Instruction
addiu rt, rs, imm
Semantics
R[rt] := R[rs] + sext(imm) if ( R[rs] != R[rt] ) pc := pc + sext(offset) + 4
Hardware Requirements
Needs adder, sext, 1w1r rf port Needs adder, sext, comparator, 2r rf port Needs adder, sext, memory read port, 1r1w rf port Needs adder, sext, memory write port, 1r1w port
L03-35
lw rt, offset(rs)
sw rt, offset(rs)
February 9, 2009
http://csg.csail.mit.edu/6.375/
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-36
Step 1: Identify the memories Step 2: Identify the datapaths Step 3: Everything else is random logic
February 9, 2009 http://csg.csail.mit.edu/6.375/ L03-37
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-38
SMIPSv1 datapath
module smipsProcDpath_pstr ( input clk, reset, // Memory ports output [31:0] imemreq_addr, output [31:0] dmemreq_addr, output [31:0] dmemreq_data, input [31:0] dmemresp_data, // Controls signals (ctrl->dpath) input pc_sel, input [ 4:0] rf_raddr0, input [ 4:0] rf_raddr1, input rf_wen, input [ 4:0] rf_waddr, input op0_sel, input op1_sel, input [15:0] inst_imm, input wb_sel, // Control signals (dpath->ctrl) output branch_cond_eq, output [7:0] tohost_next ); wire [31:0] branch_targ; wire [31:0] pc_plus4; wire [31:0] pc_out; vcMux2#(32) pc_mux ( .in0 (pc_plus4), .in1 (branch_targ), .sel (pc_sel), .out (pc_out) ); wire [31:0] pc; vcRDFF_pf#(32,32'h0001000) pc_pf ( .clk (clk), .reset_p (reset), .d_p (pc_out), .q_np (pc) ); assign imemreq_addr = pc; vcInc#(32,32'd4) pc_inc4 ( .in (pc), .out (pc_plus4) );
L03-39
February 9, 2009
http://csg.csail.mit.edu/6.375/
casez performs simple pattern matching and can be very useful when implementing decoders
op1 mux sel op1_rd0, op1_pc4, op1_rd0, op1_rd0, op1_x, wb mux sel wmx_alu, wmx_x, wmx_mem, wmx_x, wmx_x, rfile wen 1'b1, 1'b0, 1'b1, 1'b0, 1'b0, mreq r/w mreq_x, mreq_x, mreq_r, mreq_w, mreq_x, mreq val 1'b0, 1'b0, 1'b1, 1'b1, 1'b0, tohost en 1'b0}; 1'b0}; 1'b0}; 1'b0}; 1'b1};
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-41
February 9, 2009
http://csg.csail.mit.edu/6.375/
L03-43