比较器:
使用()?():()实现:
module compare(equal, a ,b);
input a ,b;
output equal;
assign equal = ( a == b )? 1: 0 ;
endmodule
Testbench:
`timescale 1 ns /100 ps
//`include "./compare.v"
module comparetest;
reg a,b;
wire equal;
initial
begin
a = 0;
b = 0;
#100 a = 0; b = 0;
#100 a = 0; b = 1;
#100 a = 1; b = 0;
#100 a = 1; b = 1;
#100 $stop;
end
// assign statements (if any)
compare compare1 (
// port map - connection between master ports and signals/registers
.a(a),
.b(b),
.equal(equal)
);
endmodule
多功能运算:
`define plus 3'd0
`define minus 3'd1
`define band 3'd2
`define bor 3'd3
`define unegate 3'd4
module alu(out,opcode ,a,b);
output [7:0] out;
reg[7:0] out;
input [2:0] opcode;
input [7:0] a,b;
always @(opcode or a or b)
begin
case(opcode)
`plus : out = a + b;
`minus : out = a - b;
`band : out = a & b;
`bor: out = a|b;
`