Verilog HDL Verilog HDL 邏 令 列邏 路 例 練
數 度 (top-down design) 行 (concurrency)
2.1 Verilog HDL (module) 邏 HDL 理
HDL 邏 料 數 邏 邏 路 module module_name (port_list) // 列 <port_declarations> // <wire_register declaration> // <parameter_declarations> // 參數 <statements> // 邏 endmodule
料 例 module and (a, b, c) input a, b // output c assign c = a & b; // 邏 endmodule
2.1.1 ) ) 例 wire w1, w2 // wire [31 0] w3, w4 // 32 reg a // 1 reg [7 0] b,c // 8 (input) (output) (inout) [msb lsb] 若 ( 例 always )
2.1.2 讀 讀 料 讀 例 reg [10 0] counter // 11 reg a // 1 reg [2 0] b // 3 a = counter[9] //counter 9 a b = counter[5 3] //counter 5 3 b reg [10 0] table[0 31] //table 11 32 table[11] = 68 // 68 table 12
2.1.3 參數 例 module vadder (a, b, c) // 路 16 parameter wlen = 15 input [wlen 0] a, b output [wlen + 1 0] c assign c = a + b endmodule module tben1 // 路 vadder #(31) ad1(a1, b1, c1) // vadder #(63) ad2(a2, b2, c2) // endmodule
例 module tben2 vadder ad1(a1, b1, c1) vadder ad2(a2, b2, c2) defparam ad1.wlen = 31, ad2.wlen = 63 //ad1 ad2 更 32 64 endmodule
2.2 Verilog HDL 邏 Begin End 林 林 Default If Always Initial
2.2.1 (Module Instantiation) 兩 module_name instance_name(port1, port2, ); module_name instance_name(.mod_port1(inst_port1),.mod_port2(inst_port2), ); 例 module adder (a, b, c) // input a, b output c <expressions> endmodule module top (a1, a2, b1, c2) // adder input a1, a2, b1 output c2 wire c1 adder adder_1 (a1, b1, c1) // adder adder_2 (.a(a2),.c(c2),.b(c1)) // endmodule
例 moduledriver (out, in, en) output [2 0] out input [2 0] in input en buf u2 (out[2], in[2], en) buf u1 (out[1], in[1], en) buf u0 (out[0], in[0], en) endmodule moduledriver (out, in, en) output [2 0] out input [2 0] in input en buf u[2 0] (out, in, en) // u[2] u[1] u[0] endmodule
2.2.2 Verilog 邏 and nand or nor xor xnor buf tran not 2.2.3 令 (Operators in assignments) 降 列 1. (concatenation and replications) { } {{ }} 2. (unary)! ~ & ˆ 3. 數 (arithmetic) * / % 4. 數 (arithmetic) + - 5. (bitwise shift) << >> 6. (relational) > >= < <= 7. (equality) = = = = = = = = 8. 邏 (binary logical) &&!! 9. 邏 (binary bitwise) & ˆ ˆ 10. (conditional)
例 wire [15 0] new_word = {word[7 0],word[15 8]}; //byte 例 module add_32 (co, sum, a, b, ci) //32 output co output [31 0] sum input [31 0] a, b input ci assign {co, sum} = a + b + ci endmodule 例 initial begin byte = { 4 { 2 b01}} // 兩 01 01010101 word = {{ 8 { byte[7]}}, byte} // end
<LHS> = <condition> <if_expression> <else_expression> 例 // assign out = enable in bz 例 // wire [3 0] mux_out, in1, in2, in3, in4 wire [1 0] sel assign mux_out = (sel = = 2 b00) in1 (sel = = 2 b01) in2 (sel = = 2 b10) in3 (sel = = 2 b11) in4 4 bx
2.2.4 令 2.2.4.1 連 令 (Continuous assignments) assign 連 (procedural block) 邏 數 不 數
例 wire out assign out = a & b // 數 wire #10 inv = inv // 數 wire and_net = a1 & a2, or_net = a1 a2 // 兩 assign out[7 4] = in1[4 1] //
2.2.4.2 塞 塞 (Blocking and Non-blocking Assignments 塞 令 = 句 數 路 利 行 路 路 不 塞 令 <= 句 行 塞 令
例 // out 路 不 always @(posedge clk) out = outˆa1 always @(posedge clk) a1 = outˆin // 利 begin end 行 ok reg temp always @(posedge clk) temp = dout dout = tempˆa1 a1 = tempˆin end // 塞 令 <= always @(posedge clk) out <= outˆa1 always @(posedge clk) a1 <= outˆin begin
2.2.5 (Conditional Statements) 2.2.5.1 If and If-else
2.2.5.2 Case
2.2.6 (Procedural Blocks) 行 initial always 兩 begin end 列 行 錄 (activation list) (timing control, TC) 行
例 #n n 行 wait 數 立 行 @ 行 or posedge negedge
2.2.6.1 Initial initial 行 initial ( $display $gr_waves) 2.2.6.2 Always always 路 連 行 錄 (activation list) (reg)
例
2.3 令 (Compiler Directives)
2.4 列邏 路 2.4.1 狀 (Finite-State Machines)
2.4.2 狀 狀 (FSM State Transition Diagram )
2.4.3 狀 Verilog HDL
2.5 例 2.5.1 Moore Machine
2.5.2 Mealy Machine
2.6 練 1. Verilog HDL 立 I. 2-3 II. III. 2-4 D 立 D 2-5