Ring or vibrate
根据逻辑表达式得到:
module top_module (
input ring,
input vibrate_mode,
output ringer, // Make sound
output motor // Vibrate
);
assign ringer = ring & ~(vibrate_mode);
assign motor = ring & vibrate_mode;
endmodule
也可以根据真值表进行always case
256to1 multiplexer
怎么理解?说不上来,读者有感受的评论交流下。
module top_module(
input [255:0] in,
input [7:0] sel,
output out );
assign out = in[sel];
endmodule
256to1 4bit mux
-: 从此处开始的低四位, +:从此处开始的高四位
module top_module(
input [1023:0] in,
input [7:0] sel,
output [3:0] out );
assign out = in[(sel *4) +: 4];
endmodule
3bit binary adder
always中不可以例化,例化与always,always与always之间都是并行关系。
module top_module(
input [2:0] a, b,
input cin,
output [2:0] cout,
output [2:0] sum );
wire [3:0] cout_mid;
assign cout_mid[0] = cin;
generate
genvar i;
for (i = 0; i < 3; i = i + 1)
begin:loop
add x (a[i], b[i], cout_mid[i], cout_mid[i + 1], sum[i]);
end
endgenerate
assign cout = cout_mid[3:1];
endmodule
module add(
input a,
input b,
input cin,
output cout,
output sum
);
assign {cout, sum} = a + b + cin;
endmodule
Signed addition overflow
两个正数相加符号位为1,或两个负数相加符号位为0,说明溢出了。
module top_module (
input [7:0] a,
input [7:0] b,
output [7:0] s,
output overflow
); //
assign s = a + b;
assign overflow = ((~ a[7] && ~ b[7]) && s[7]) | ((a[7] && b[7]) && (~s[7]));
endmodule
minimum SOP POS
SOP:(sum of product) 最小项的和(圈1)
POS(p… of sum) 最大项的积(圈0取反)
最终代表同一逻辑。
module top_module (
input a,
input b,
input c,
input d,
output out_sop,
output out_pos
);
assign out_sop = (c & d) | (~a & ~b & c);
assign out_pos = ~((~c) | (a & c & ~d) | (b & c & ~d));
endmodule