module top #(parameter WIDTH =32 )(seg7,scan,clk);//????
output [7:0]seg7;
input clk;
output [3:0]scan;
// reg clk;
reg clk1; //????
reg reset,reset1;
// reg button;
wire memread,memwrite;
wire [WIDTH-1:0] adr,writedata;
wire [WIDTH-1:0] memdata;
wire [WIDTH-1:0] rd3;
wire[3:0] scan ;
wire [7:0] seg7 ;
reg count1=0;
reg clk1=0;
//always@(negedge button)
//begin
//end
always @ (posedge clk)
begin
if(count1==1)
begin clk1<=~clk1;
count1<=0;end
else
begin count1<=count1+1;end
end
/*always @ (posedge clk)
begin
if(reset1==0)
begin clk1<=~clk1;end
end*/
//instantiate devices to be tested 需要仿真的CPU模块
mips #(WIDTH) dut(clk1,reset,memdata,memread,memwrite,adr,writedata,rd3);
xianshi a(clk1,rd3,seg7,scan);
//exmemory 存储器模块
exmemory #(WIDTH) exmem(clk1,reset,memwrite,adr,writedata,memdata);
//initial
initial
begin
reset<=0;
// button<=1;
clk1<=0;
// #50;
//button<=0;
/* #500;
reset<=1;
#50
reset<=0;*/
//# 50;
// reset <=0;
end
//generate clock
//always
//begin
//clk <=1;
//# 5;
//clk <=0;
//# 5;
//end
/*always @(negedge clk)
begin
if(memwrite)
if(adr==5&writedata==7)
$display("Simulation completely successful");
else
$display("Simulation failed");
end*/
endmodule
module xianshi(clk,t1,seg7,scan);
input clk;
input [31:0]t1;
output seg7;
output scan;
reg[3:0] data;
reg[31:0] num;
reg[3:0] scan ;
reg [7:0] seg7 ;
reg[1:0] cnt=0;
reg clk1khz=0;
reg[14:0] count1=0;
always @ (posedge clk)
begin
if(count1==25000)
begin clk1khz<=~clk1khz;
count1<=0;end
else
begin count1<=count1+1;end
end
always @(posedge clk1khz)
begin
if(cnt==3)
begin cnt<=0;end
else
begin cnt<=cnt+1;end
end
/*always @(t1)
begin
num<=t1;
end*/
always @ (cnt)
begin
case(cnt)
2'b00:begin data[3:0]<=t1[3:0];scan[3:0]<=4'b1110/*&{4{blink[0]}}*/;end
2'b01:begin data[3:0]<=t1[7:4];scan[3:0]<=4'b1101/*&{4{blink[0]}}*/;end
2'b10:begin data[3:0]<=t1[11:8];scan[3:0]<=4'b1011/*&{4{blink[1]}}*/;end
2'b11:begin data[3:0]<=t1[15:12];scan[3:0]<=4'b0111/*&{4{blink[1]}}*/;end
//default:begin data<=4'bx;scan<=4'bx;end
endcase
end
always @ (data)
begin
case(data[3:0])
4'b0000:seg7[7:0]=8'b11000000;//0
4'b0001:seg7[7:0]=8'b11111001;//1
4'b0010:seg7[7:0]=8'b10100100;//2
4'b0011:seg7[7:0]=8'b10110000;//3
4'b0100:seg7[7:0]=8'b10011001;//4
4'b0101:seg7[7:0]=8'b10010010;//5
4'b0110:seg7[7:0]=8'b10000010;//6
4'b0111:seg7[7:0]=8'b11111000;//7
4'b1000:seg7[7:0]=8'b10000000;//8
4'b1001:seg7[7:0]=8'b10010000;//9
4'b1010:seg7[7:0]=8'b10001000;//a
4'b1011:seg7[7:0]=8'b10000011;//b
4'b1100:seg7[7:0]=8'b11000110;//c
4'b1101:seg7[7:0]=8'b10100001;//d
4'b1110:seg7[7:0]=8'b10000110;//e
4'b1111:seg7[7:0]=8'b11111111;//f
//4'b1111:seg7[7:0]=8'b10001110;//f
default:seg7[7:0]=8'b11111111;//11111111;
endcase
end
endmodule
//external memory
module exmemory #(parameter WIDTH =32)
(clk,reset,memwrite,adr,writedata,memdata);
input clk;
input reset;
input memwrite;
input [WIDTH-1:0] adr,writedata;
output reg [WIDTH-1:0] memdata;
reg [31:0] RAM [2047:0];
wire [31:0] word;
initial
begin
$readmemh("C:/Users/Administrator/Desktop/huibian/flower.dat",RAM);
//RAM[0] <=32'b00000001000010010101000001000000;//SLL
/*RAM[1] <=32'b00000001000010010101000001000010;//SRL
RAM[2] <=32'b00000001000010010101000001000011;//SRA
RAM[3] <=32'b00000001000010010101000001000100;//SLLV
RAM[4] <=32'b00000001000010010101000001000111;//SRAV
RAM[5] <=32'b00000001000010010101000001100000;//Add
RAM[6] <=32'b00000001001010000101000001100010;//Sub
RAM[7] <=32'b00000001000010010101000001100001;//Addu
RAM[8] <=32'b00000001001010000101000001100011;//Subu
RAM[9] <=32'b00000001000010010101000001100110;//Xor
RAM[10]<=32'b00000001000010010101000001100100;//And
RAM[11]<=32'b00000001000010010101000001100101;//Or
RAM[12]<=32'b00000001000010010101000001100111;//Nor
RAM[13]<=32'b00000001000010010101000001101010;//SLT
RAM[14]<=32'b00100001000010010000000000000001;//Addi
RAM[15]<=32'b00100101000010010000000000000001;//Addiu
RAM[16]<=32'b00101001000010010000000000000001;//SLTI
RAM[17]<=32'b00110001000010010000000000000001;//Andi
RAM[18]<=32'b00110101000010010000000000001111;//Ori
RAM[19]<=32'b00111001000010010000000000000001;//Xori
RAM[20]<=32'b00111101000010010000000011111111;//LUI
RAM[21] <=32'b10001100000010010000000000000011;//LW
RAM[22] <=32'b10101100000010110000000000000011;//SW
RAM[23] <=32'b00000001000010010101000000001000;//Jr
RAM[24] <=32'b00001000000000000000000000000101;//J
RAM[25] <=32'b00000101000010010000000000000011;//Bltz
RAM[26] <=32'b00010001000010000000000000000011;//Beq
RAM[27] <=32'b00011101000000000000000000000111;//Bgtz
*/
//RAM[0] <=32'b00111101000010010000000011111111;//LUI
// RAM[0] <=32'b10001100000010010000000000000011;//LW
// RAM[0] <=32'b10101100000010110000000000000011;//SW
// RAM[0] <=32'b00000001000010010101000000001000;//Jr
// RAM[0] <=32'b00001000000000000000000000000101;//J
// RAM[0] <=32'b00000101000010010000000000000011;//Bltz
// RAM[0] <=32'b00010001000010000000000000000011;//Beq
// RAM[0] <=32'b00011101000000000000000000000111;//Bgtz
// RAM[0] <=32'b10101100000010110000000000000011;//SW
// RAM[0] <=32'b10001100000010010000000000000011;//LW
/* RAM[1] <=32'b00000001000010010101000001000010;//SRL
RAM[2] <=32'b00000001000010010101000001000011;//SRA
RAM[3] <=32'b00000001000010010101000001000100;//SLLV
RAM[4] <=32'b00000001000010010101000001000111;//SRAV
RAM[5] <=32'b00000001000010010101000001100000;//Add
RAM[6] <=32'b00000001001010000101000001100010;//Sub
RAM[7] <=32'b00000001000010010101000001100001;//Addu
RAM[8] <=32'b00000001001010000101000001100011;//Subu
RAM[9] <=32'b00000001000010010101000001100110;//Xor
RAM[10]<=32'b00000001000010010101000001100100;//And
RAM[11]<=32'b00000001000010010101000001100101;//Or
RAM[12]<=32'b00000001000010010101000001100111;//Nor
RAM[13]<=32'b00000001000010010101000001101010;//SLT
*/
//RAM[0] <=32'b00100001000010010000000000000001;//Addi
// RAM[0] <=32'b00101001000010010000000000000001;//SLTI
// RAM[0] <=32'b00100101000010010000000000001001;//Addiu
// RAM[0] <=32'b00110001000010010000000000000001;//Andi
// RAM[1] <=32'b00110101000010010000000000001111;//Ori
// RAM[2] <=32'b00111001000010010000000000000001;//Xori
// RAM[0] <=32'b00110001000010010000000000001000;//Andi
// RAM[1] <=32'b00110101000010010000000000000111;//Ori
// RAM[2] <=32'b00111001000010010000000000000110;//Xori
//1+2+3+4+5+6+7
/*RAM[0] <=32'b00100000000010010000000000000001;//rt<=1
RAM[1] <=32'b00100000000010100000000000000000;//rd<=0
RAM[2] <=32'b00000001001010100101000000100000;//rt+rd=>rd
RAM[3] <=32'b00100001001010010000000000000001;
RAM[4] <=32'b00000001000010010101100000100010;
RAM[5] <=32'b00010001011000000000000000000111;
RAM[6] <=32'b00001000000000000000000000000010;
RAM[7] <=32'b10101100000010110000000000000011;*/
/*RAM[0] <=32'b10001101110011010000000000000000;//取外设
RAM[1] <=32'b00000000000011010110110000000000;//左移位
RAM[2] <=32'b00000000000011010100110000000010;//右
RAM[3] <=32'b10001101000010100000000000000000;//取内存数
RAM[4] <=32'b00010001000010110000000000000111;//判断地址
RAM[5] <=32'b00010001001010100000000000001000;//判断两个数是否相等
RAM[6] <=32'b00000001000011110100000000100000;//地址+1
RAM[7] <=32'b00001000000000000000000000000011;//j
RAM[12]<=32'b00000000001000001000000000100000;//失败操作
RAM[14]<=32'b00000001000000001000000000100000;//成功操作
RAM[13]<=32'b00001000000000000000000000001101;
RAM[15]<=32'b00001000000000000000000000001111;
RAM[16]<=32'b00000000000000000000000000000001;
RAM[17]<=32'b00000000000000000000000000000010;
RAM[18]<=32'b00000000000000000000000000000011;
RAM[19]<=32'b00000000000000000000000000000100;
RAM[20]<=32'b00000000000000000000000000000101;
RAM[21]<=32'b00000000000000000000000000000110;
RAM[26]<=32'b00000000000000000000000000000011;
// RAM[31]<=32'b00000000000000001010101111001101;*/
end
always @(posedge clk)
if(memwrite)
RAM[adr] <= writedata;
assign word =RAM[adr];
always @(*)
memdata <=word;
endmodule
//mips
module mips #(parameter WIDTH=32) //?????
(input clk,reset,
input [WIDTH-1:0] memdata,
output memread,memwrite,
output [WIDTH-1:0] adr,writedata,rd3);
wire [31:0] instr;//IR
wire memtoreg,irwrite,iord,pcen,regwrite,regdst,zero;
wire [1:0] alusrca,alusrcb,pcsource;
wire [5:0] aluop;
//CU
controller cont(clk,reset,instr[31:26],instr[5:0],
zero,memread,memwrite,memtoreg,iord,irwrite,
pcen,regwrite,regdst,pcsource,alusrca,alusrcb,
aluop);
//datapath
datapath #(WIDTH) dp(clk,reset,memdata,memtoreg,iord,pcen,regwrite,regdst,
irwrite,alusrca,alusrcb,pcsource,aluop,zero,
instr,adr,writedata,rd3);
endmodule
//CU
module controller(input clk,reset, //?????
input [5:0] op,
input [5:0] func,
input zero,
output reg memread,memwrite,memtoreg,iord,irwrite,
output pcen,
output reg regwrite,regdst,
output reg [1:0] pcsource,alusrca,alusrcb,
output reg[5:0] aluop);
//state
parameter FETCHS =4'b0000;
parameter DECODES=4'b0001;
parameter MTYPES =4'b0010;
parameter ITYPES =4'b0011;
parameter JRUMPS =4'b0100;
parameter BEQS =4'b0101;
parameter BLTZS =4'b0110;
parameter BGTZS =4'b0111;
parameter JUMPS =4'b1000;
parameter ReadMS =4'b1001;
parameter WriteMS=4'b1010;
parameter IWriteToRegS=4'b1011;
parameter RITYPES=4'b1100;
parameter ROTYPES=4'b1101;
parameter MWriteToRegS=4'b1110;
parameter RWriteToRegS=4'b1111;
//OP[5:0]
parameter RType =6'b000000;
parameter Bltzop =6'b000001;
parameter Jop =6'b000010;
parameter Beqop =6'b000100;
parameter Bgtzop =6'b000111;
parameter Addiop =6'b001000;
parameter Addiuop =6'b001001;
parameter Sltiop =6'b001010;
parameter Adiop =6'b001100;
parameter Oriop =6'b001101;
parameter Xoriop =6'b001110;
parameter Luiop =6'b001111;
parameter Lwop =6'b100011;
parameter Swop =6'b101011;
//func
parameter Func1 =6'b000000;
parameter Func2 =6'b000010;
parameter Func3 =6'b000011;
parameter Func4 =6'b000100;
parameter Func5 =6'b000111;
parameter Func6 =6'b001000;
parameter Func7 =6'b100000;
parameter Func8 =6'b100001;
parameter Func9 =6'b100010;
parameter Func10 =6'b100011;
parameter Func11 =6'b100110;
parameter Func12 =6'b100100;
parameter Func13 =6'b100101;
parameter Func14 =6'b100111;
parameter Func15 =6'b101010;
reg [3:0] state,nextstate;
reg pcwrite,pcwritecond;
always @(posedge clk )
begin
if(reset)
state <=FETCHS;
// state <=FETCHS;
else
state <=nextstate;
end
always @(*)
begin
case(state)
FETCHS : nextstate <=DECODES;
DECODES: case(op)
RType:
case(func)
Func1: nextstate<=RITYPES;
Func2: nextstate<=RITYPES;
Func3: nextstate<=RITYPES;
Func4: nextstate<=ROTYPES;
Func5: nextstate<=ROTYPES;
Func6: nextstate<=JRUMPS;
Func7: nextstate<=ROTYPES;
Func8: nextstate<=ROTYPES;
Func9: nextstate<=ROTYPES;
Func10: nextstate<=ROTYPES;
Func11: nextstate<=ROTYPES;
Func12: nextstate<=ROTYPES;
Func13: nextstate<=ROTYPES;
Func14: nextstate<=ROTYPES;
Func15: nextstate<=ROTYPES;
endcase
Bltzop: nextstate <=BLTZS;
Jop: nextstate <=JUMPS;
Beqop: nextstate<=BEQS;
Bgtzop: nextstate<=BGTZS;
Addiop: nextstate<=ITYPES;
Addiuop: nextstate<=ITYPES;
Sltiop: nextstate<=ITYPES;
Adiop: nextstate<=ITYPES;
Oriop: nextstate<=ITYPES;
Xoriop: nextstate<=ITYPES;
Luiop: nextstate<=ITYPES;
Lwop: nextstate<=MTYPES;
Swop: nextstate<=MTYPES;
default:nextstate<=FETCHS;
endcase
ITYPES: nextstate<=IWriteToRegS;
RITYPES: nextstate<=RWriteToRegS;
ROTYPES: nextstate<=RWriteToRegS;
BLTZS: nextstate<=FETCHS;
BEQS: nextstate<=FETCHS;
BGTZS: nextstate<=FETCHS;
JUMPS: nextstate<=FETCHS;
JRUMPS: nextstate<=FETCHS;
MTYPES: case(op)
Lwop:
nextstate<=ReadMS;
Swop:
nextstate<=WriteMS;
endcase
ReadMS: nextstate<=MWriteToRegS;
WriteMS: nextstate<=FETCHS;
IWriteToRegS: nextstate<=FETCHS;
MWriteToRegS: nextstate<=FETCHS;
RWriteToRegS: nextstate<=FETCHS;
default: nextstate<=FETCHS;
endcase
end
always @(*)
begin
irwrite <= 0;
pcwrite <= 0; pcwritecond <= 0;
regwrite <= 0; regdst <= 0;
memread <= 0; memwrite <= 0;
alusrca <= 2'b00; alusrcb <= 2'b00; aluop <= 6'b100000;
pcsource <= 2'b00;
iord <= 0; memtoreg <= 0;
case(state)
FETCHS:
begin
iord<=0;
irwrite<=1;
memread<=1;
memwrite<=0;
alusrca<=2'b00;
alusrcb<=2'b01;
pcsource<=2'b00;
pcwrite<=1;
aluop<=6'b100000;
end
DECODES:
begin
aluop<=6'b100000;
alusrca<=2'b00;
alusrcb<=2'b11;
end
MTYPES:
begin
alusrca<=2'b01;
alusrcb<=2'b11;
aluop<=6'b100000;
end
ITYPES:
begin
alusrca<=2'b01;
alusrcb<=2'b11;
case(op)
Addiop:
aluop<=6'b100000;
Addiuop:
aluop<=6'b100001;
Sltiop:
aluop<=6'b101010;
Adiop:
aluop<=6'b100100;
Oriop:
aluop<=6'b100101;
Xoriop:
aluop<=6'b100110;
Luiop:
aluop<=6'b010001;
endcase
end
JRUMPS:
begin
pcwrite<=1;
pcsource<=2'b11;
end
BEQS:
begin
alusrca<=2'b01;
alusrcb<=2'b00;
aluop<=6'b100010;
pcsource<=2'b01;
pcwritecond<=1;
end
BLTZS:
begin
alusrca<=2'b01;
alusrcb<=2'b00;
aluop<=6'b000001;
pcsource<=2'b01;
pcwritecond<=1;
pcwrite<=0;
end
BGTZS:
begin
alusrca<=2'b01;
alusrcb<=2'b00;
aluop<=6'b001010;
pcsource<=2'b01;
pcwritecond<=1;
pcwrite<=0;
end
JUMPS:
begin
pcwrite<=1;
pcsource<=2'b10;
end
ReadMS:
begin
memread<=1;
iord<=1;
end
WriteMS:
begin
memwrite<=1;
iord<=1;
end
IWriteToRegS:
begin
memtoreg<=0;
regwrite<=1;
regdst<=0;
end
RITYPES:
begin
alusrca<=2'b10;
alusrcb<=2'b00;
case(func)
Func1: aluop<=6'b000000;
Func2: aluop<=6'b000010;
Func3: aluop<=6'b000011;
endcase
end
ROTYPES:
begin
alusrca<=2'b01;
alusrcb<=2'b00;
case(func)
Func4: aluop<=6'b000000;
Func5: aluop<=6'b000010;
Func7: aluop<=6'b100000;
Func8: aluop<=6'b100001;
Func9: aluop<=6'b100010;
Func10:aluop<=6'b100011;
Func11:aluop<=6'b100110;
Func12:aluop<=6'b100100;
Func13:aluop<=6'b100101;
Func14:aluop<=6'b100111;
Func15:aluop<=6'b101010;
endcase
end
MWriteToRegS:
begin
regdst<=0;
regwrite<=1;
memtoreg<=1;
end
RWriteToRegS:
begin
regdst<=1;
regwrite<=1;
memtoreg<=0;
end
endcase
end
assign pcen =pcwrite|(pcwritecond & zero);
endmodule
//datapath
module datapath #(parameter WIDTH =32 )
(input clk,reset,
input [WIDTH-1:0] memdata,
input memtoreg,iord,pcen,regwrite,regdst,irwrite,
input [1:0] alusrca,alusrcb,pcsource,
input [5:0] aluop,
output zero,
output [31:0] instr,
output [WIDTH-1:0] adr,writedata,rd3);
parameter CONST_ZERO = 32'b0;
parameter CONST_ONE = 32'b1;
wire [4:0] ra1,ra2,wa;//
wire [WIDTH-1:0] pc,nextpc,md,rd1,rd2,wd,a,src1,src2,aluresult,aluout;
wire [31:0] jp1;
assign jp1 ={6'b000000,instr[25:0]};
wire [31:0] ta1,ta2;
assign ta1 ={27'b0,instr[10:6]};
assign ta2 ={16'b0,instr[15:0]};
assign ra1 =instr[25:21];
assign ra2 =instr[20:16];
mux2 regmux(instr[20:16],instr[15:11],regdst,wa);
flopen #(32) ir(clk,irwrite,memdata,instr);
//datapath
flopenr #(WIDTH) pcreg(clk,reset,pcen,nextpc,pc);
flop #(WIDTH) mdr(clk,memdata,md);
flop #(WIDTH) areg(clk,rd1,a);
flop #(WIDTH) wrd(clk,rd2,writedata);
flop #(WIDTH) res(clk,aluresult,aluout);
mux2 #(WIDTH) adrmux(pc,aluout,iord,adr);
mux4 #(WIDTH) src1mux(pc,a,ta1,ta2,alusrca,src1);
mux4 #(WIDTH) src2mux(writedata,CONST_ONE,ta1,ta2,alusrcb,src2);
mux4 #(WIDTH) pcmux(aluresult,aluout,jp1,rd1,pcsource,nextpc);
mux2 #(WIDTH) wdmux(aluout,md,memtoreg,wd);
regfile #(WIDTH) rf(clk,reset,regwrite,ra1,ra2,wa,wd,rd1,rd2,rd3);
alu #(WIDTH) alunit(src1,src2,aluop,aluresult);
zerodetect #(WIDTH) zd(aluresult,zero);
endmodule
//ALU ??????
module alu #(parameter WIDTH=32)
(
input [WIDTH-1:0] a,b,
input [5:0] aluop,
output reg [WIDTH-1:0] result
);
wire [30:0] b2;
assign b2=a[30:0];
wire [WIDTH-1:0] sum,slt,shamt;
always @(*)
begin
case(aluop)
6'b000000: result<=(b<
6'b000010: result<=(b>>a);
6'b000011: result<=(b>>>a);
6'b001000: result<= 32'b0;
6'b100000: result<=(a+b);
6'b100001: result<=(a+b);
6'b100010: result<=(a-b);
6'b100011: result<=(a-b);
6'b100110: result<=(a^b);
6'b100100: result<=(a&b);
6'b100101: result<=(a|b);
6'b100111: result<=~(a&b);
6'b101010: result<=(a
6'b000001: //Bltz
begin
result<=(a<0 ? 0:1);
end
6'b001010: //Bgtz
begin
result<=(a>0 ? 0:1);
end
6'b010001: result<=((b<<16)& 32'b11111111111111110000000000000000);//LUI
endcase
end
endmodule
//regfile
module regfile #(parameter WIDTH=32,REGBITS=5)
(input clk,
input reset,
input regwrite,
input [REGBITS-1:0] ra1,ra2,wa,
input [WIDTH-1:0] wd,
output [WIDTH-1:0] rd1,rd2,rd3);
reg [WIDTH-1:0] RAM2 [(1<
initial
begin
//$readmemh("regfile.dat",RAM);
RAM2[0] <=32'b00000000000000000000000000000000;
RAM2[1] <=32'b11111111111111111111111111111111;
RAM2[8] <=32'b00000000000000000000000000010000;//地址 %
RAM2[9] <=32'b00000000000000000000000000000000;//要找的数
RAM2[10]<=32'b00000000000000000000000000000000;//内存中取来的数
RAM2[15]<=32'b00000000000000000000000000000001;//常数1
RAM2[12]<=32'b00000000000000000000001000000000;
RAM2[11]<=32'b00000000000000000000000000010110;//末位+1的地址 %
RAM2[13]<=32'b00000000000000000000000000000000;//读入的数暂存
RAM2[14]<=32'b00000000000000000000000000011010;//%
RAM2[16]<=32'b00000000000000000000000000000000;//数码管输出寄存器
//RAM2[31]<=32'b00000000000000001010101111001101;//00000000000000000001001000110100;
end
always @(posedge clk)
if(regwrite)
RAM2[wa]<=wd;
assign rd1 =ra1 ? RAM2[ra1]:0;
assign rd2 =ra2 ? RAM2[ra2]:0;
assign rd3 =RAM2[16];
endmodule
//zerodetect ???????0???beq??
module zerodetect #(parameter WIDTH=32)
(input [WIDTH-1:0] a,
output y);
assign y= (a==0);
endmodule
//flop ????
module flop #(parameter WIDTH =32)
(input clk,
input [WIDTH-1:0] d,
output reg [WIDTH-1:0] q);
always @(posedge clk)
q<=d;
endmodule
//flopen
module flopen #(parameter WIDTH =32)
(input clk,en,
input [WIDTH-1:0] d,
output reg [WIDTH-1:0] q);
always @(posedge clk)
if(en)
q<=d;
endmodule
//flopenr
module flopenr #(parameter WIDTH =32)
(input clk,reset,en,
input [WIDTH-1:0] d,
output reg [WIDTH-1:0] q);
always @(posedge clk)
if(reset)
q<=0;
else
if(en)
q<=d;
endmodule
//mux2
module mux2 #(parameter WIDTH =32)
(input [WIDTH-1:0] d0,d1,
input s,
output [WIDTH-1:0] y);
assign y= s ? d1:d0;
endmodule
//mux4
module mux4 #(parameter WIDTH =32)
(input [WIDTH-1:0] d0,d1,d2,d3,
input [1:0] s,
output reg [WIDTH-1:0] y);
always @(*)
case(s)
2'b00: y<= d0;
2'b01: y<= d1;
2'b10: y<= d2;
2'b11: y<= d3;
endcase
endmodule
以上为verilog源程序,上午调试的过程中发现只要把 reg clk1这一行注释掉就能够仿真,不过仿真结果不正确