1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| 取反操作 module top_module( input [7:0] in, output [7:0] out ); assign out={in[0],in[1],in[2],in[3],in[4],in[5],in[6],in[7]}; endmodule
矢量亦可以复制 {5{1'b1}} {2{a,b,c}} {3'd5, {2{3'd6}}} module top_module ( input [7:0] in, output [31:0] out );
assign out={ {24{in[7]}},in}; endmodule
多个复制问题 module top_module ( input a, b, c, d, e, output [24:0] out ); assign out=~{{5{a}},{5{b}},{5{c}},{5{d}},{5{e}}}^{5{a,b,c,d,e}}; endmodule
多模块以及选择器:(模块一定要注意对应位置不要出错,太关键了) module top_module ( input clk, input [7:0] d, input [1:0] sel, output [7:0] q ); wire [7:0] q1,q2,q3; my_dff8 a1(clk,d,q1); my_dff8 a2(clk,q1,q2); my_dff8 a3(clk,q2,q3); always @(*) begin case(sel) 2'b00:q=d; 2'b01:q=q1; 2'b10:q=q2; default:q=q3; endcase end endmodule
分线与加法器 module top_module( input [31:0] a, input [31:0] b, output [31:0] sum ); wire [15:0] sum1,sum2; wire cout1,cout2; add16 a1(a[15:0],b[15:0],0,sum1,cout1); add16 a2(a[31:16],b[31:16],cout1,sum2,cout2); assign sum={sum2,sum1}; endmodule
简单加法器 module add1 ( input a, input b, input cin, output sum, output cout ); assign sum=a^b^cin; assign cout=(a & b) | (b & cin) | (cin & a);
endmodule
加减法 需要注意的是按位与必须两者都要相同位数,则需要进行复制操作 module top_module( input [31:0] a, input [31:0] b, input sub, output [31:0] sum ); wire [31:0] b1; wire [15:0] sum1,sum2; wire cout1,cout2; assign b1=b^{32{sub}}; add16 a1(a[15:0],b1[15:0],sub,sum1,cout1); add16 a2(a[31:16],b1[31:16],cout1,sum2,cout2); assign sum={sum2,sum1}; endmodule
if语句运用 需要注意的是寄存器必须改变状态
module top_module ( input cpu_overheated, output reg shut_off_computer, input arrived, input gas_tank_empty, output reg keep_driving );
always @(*) begin if (cpu_overheated) shut_off_computer = 1; else shut_off_computer=0; end
always @(*) begin if (~arrived&&~gas_tank_empty) keep_driving = 1; else keep_driving=0; end
endmodule
|