case语句描述4选1数据选择器
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module mux4_1(out,in0,in1,in2,in3,sel);
output out;input in0,in1,in2,in3;
input [1:0] sel;
reg out;
always @ (in0 or in1 or in2 or in3 or sel)
case(sel)
2'b00:out<=in0;
2'b01:out<=in1;
2'b10:out<=in2;
2'b11:out<=in3;
default:out<=1'bx;
endcase
endmodule
同步置数,同步清零的计数器
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module count(out,data,load,reset,clk);
output [7:0] out;
input [7:0] data;
input load,reset,clk;
reg out;
always @ (posedge clk)
begin
if(!reset) out<=8'd0;
else if (load) out<=data;
else out<=out+8'd1;
end
endmodule
用always过程语句描述的简单算术逻辑单元
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
`define add 3'd1
`define minus 3'd2
`define band 3'd3
`define bor 3'd4
`define bnot 3'd5
module alu(out,opcode,a,b);
output [7:0] out;
reg [7:0] out;
input [2:0] opcode;
input [7:0] a,b;
always @ (a or b or opcode)
begin
case(opcode)
`add: out = a+b;
`minus: out=a-b;
`band: out=a&b;
`bor: out=a|b;
`bnot: out=~a;
default:out<=8'hx;
endcase
end
endmodule
遇到的问题:在使用 `define add 3'd1; 分号导致宏变量,引用替换的时候也加入的分号,结果编译出错。
Line 69: Syntax error near ";".