Cpr E 305 Lab Verilog Synthesis Handbook Vignesh Vijayakumar
Cpr E 305 Lab Verilog Synthesis Handbook
Vignesh Vijayakumar December 9, 2004 (Fall) Lab TA: Ganesh Subramanian
Page 1 of 67 Cpr E 305 Lab Verilog Synthesis Handbook Vignesh Vijayakumar
Table of Contents
Quad I nput NOR Gate.................................................................................................. 3 D Flip Flop using NOR Gates........................................................................................ 4 2:1 Mux using Gates .................................................................................................... 5 4:1 Mux using 2:1 Muxes............................................................................................. 6 Full Adder..................................................................................................................... 7 8:1 Mux using 4:1 & 2:1 Muxes ................................................................................... 8 Octal to Binary Encoder ............................................................................................. 10 4 Bit Comparator........................................................................................................ 11 4x4 Multiplier............................................................................................................. 12 4 Bit Serial I n Parallel Out Register........................................................................... 14 Serial Adder FSM........................................................................................................ 15 Sequence Detector FSM............................................................................................. 17 Single Cycle CPU........................................................................................................ 19 Multi Cycle CPU.......................................................................................................... 33
Page 2 of 67 Cpr E 305 Lab Verilog Synthesis Handbook Vignesh Vijayakumar Quad I nput NOR Gate
Page 14 of 67 Cpr E 305 Lab Verilog Synthesis Handbook Vignesh Vijayakumar Serial Adder FSM
Lab Session: 5
Verilog Code:
module serialadder(Clk, In, In2, Out);
input Clk, In, In2; output Out;
parameter C0 = 1'b0; parameter C1 = 1'b1;
reg state, nextstate, Out;
always @ (posedge Clk) begin state <= nextstate; end
always @ (In or In2 or state) begin case({state, In, In2}) 3'b000: begin nextstate = C0; Out = 1'b0; end 3'b001: begin nextstate = C0; Out = 1'b1; end 3'b010: begin nextstate = C0; Out = 1'b1; end 3'b011: begin nextstate = C1; Out = 1'b0; end 3'b100: begin nextstate = C0; Out = 1'b1; end 3'b101: begin nextstate = C1; Out = 1'b0; end 3'b110: begin nextstate = C1; Out = 1'b0; end 3'b111: begin nextstate = C1; Out = 1'b1; end endcase end
endmodule
Page 15 of 67 Cpr E 305 Lab Verilog Synthesis Handbook Vignesh Vijayakumar Serial Adder FSM
RTL:
Page 16 of 67 Cpr E 305 Lab Verilog Synthesis Handbook Vignesh Vijayakumar Sequence Detector FSM
Lab Session: 5
Verilog Code:
module sequencedetector(Reset, Clk, In, out);
input Reset, Clk, In; output out;
parameter A = 2'b00, B = 2'b01, C = 2'b10, D = 2'b11;
reg out; reg[1:0] state, nextstate;
always@ (posedge Clk or posedge Reset) begin if(Reset == 1'b1) state <= A; else state <= nextstate; end
always@ (In or state) begin case({state, In}) //State A 3'b000: begin nextstate = B; out = 1'b0; end 3'b001: begin nextstate = A; out = 1'b0; end //State B 3'b010: begin nextstate = B; out = 1'b0; end 3'b011: begin nextstate = C; out = 1'b0; end //State C 3'b100: begin nextstate = D; out = 1'b0; end 3'b101: begin nextstate = A; out = 1'b0; end //State D 3'b110:
Page 17 of 67 Cpr E 305 Lab Verilog Synthesis Handbook Vignesh Vijayakumar begin nextstate = B; out = 1'b0; end 3'b111: begin nextstate = C; out = 1'b1; end
endcase end endmodule
RTL:
Page 18 of 67 Cpr E 305 Lab Verilog Synthesis Handbook Vignesh Vijayakumar Single Cycle CPU
// Instantiate your microcode control unit here MCCtrl MCCTRLRESULT(inst[31:26], rom_out[9:0]);
// ALU control block ALUCtrl ALUCTRLRESULT(aluop, inst[5:0], alu_con);
// get register numbers // Decode instruction to get register number locations here assign reg_a = inst[25:21]; assign reg_b = inst[20:16]; assign reg_c = inst[15:11];
// get immediate value // Decode instruction to get the immediate value here SignExtend SIGNEXTENDRESULT(inst[15:0], imm_value);
// Register file: read and writeback // Instantiate your register file here reg32bit REG32BITRESULT(clock, regwrite, reg_a, reg_b, regdst ? reg_c : reg_b, val_c, val_a, val_b);
// main ALU // Instantiate your ALU here MIPSALU MIPSALURESULT(alu_con, val_a, alusrc ? imm_value : val_b, alu_out, zero);
// branch target adder // Instantiate the adder for the branch target here assign branch_target = pc_plus_4 + (imm_value << 2);
assign Zero = (ALUOut == 0); //Zero is true if ALUOut is 0
always @ (ALUctl or A or B) begin //reevaluate if these change case (ALUctl) 4'd0: ALUOut = A & B; 4'd1: ALUOut = A | B; 4'd2: ALUOut = A + B; 4'd6: ALUOut = A - B; 4'd7: ALUOut = A < B ? 32'd1 : 32'd0; 4'd12: ALUOut = ~(A | B); // result is nor default: ALUOut = 4'd0; //nothing endcase end endmodule
always@ (MemRead or MemWrite or Address or WrData) begin if(MemRead == 1'b1) RdData = Mem[Address]; if(MemWrite == 1'b1) Mem[Address] = WrData; end endmodule
assign Zero = (ALUOut == 0); //Zero is true if ALUOut is 0
always @ (ALUctl or A or B) begin //reevaluate if these change case (ALUctl) 4'd0: ALUOut = A & B; 4'd1: ALUOut = A | B; 4'd2: ALUOut = A + B; 4'd6: ALUOut = A - B; 4'd7: ALUOut = A < B ? 32'd1 : 32'd0; 4'd12: ALUOut = ~(A | B); // result is nor default: ALUOut = 4'd0; //nothing endcase end endmodule