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 ";".