├── .gitignore ├── 1 - Basics ├── 03 - Simple Wire.v ├── 02 - Output Zero.v ├── 05 - Not Gate.v ├── 08 - XNOR Gate.v ├── 06 - And Gate.v ├── 01 - Getting Started.v ├── 07 - Nor Gate.v ├── 04 - Four Wires.v ├── 09 - Declaring Wires.v └── 10 - 7458 Chip Design.v ├── 3 - Modules ├── 1 - Modules.v ├── 2 - Connect ports by position.v ├── 4 - Connecting Three Modules.v ├── 6 - Adder1.v ├── 3 - Connect by Name.v ├── 7 - adder2-design a 1-bit adder.v ├── 9 - Adder Subtractor.v ├── 8 - Carry Select Adder.v └── 5 - Modules and Vectors.v ├── 6 - Combinational Logic ├── 1- Basic Gates │ ├── 2 - GND.v │ ├── 1- Wire.v │ ├── 10 - Simple circuit A.v │ ├── 11 - Simple circuit B.v │ ├── 3 - NOR.v │ ├── 9 - Two-bit equality.v │ ├── 4 - Another gate.v │ ├── 5 - Two Gates.v │ ├── 7 - 7420 chip.v │ ├── 8 - Truth table.v │ ├── 13 - Ring or vibrate.v │ ├── 15 - 3-bit population count.v │ ├── 14 - Thermostat.v │ ├── 16 - Gates and vectors.v │ ├── 17 - Even longer vectors.v │ ├── 6 - More logic gates.v │ └── 12 - Combine Circuits A and B.v ├── 2 - Multiplexers │ ├── 1 - 2 to 1 Multiplexer.v │ ├── 2 - 2 to 1 Bus multiplexer.v │ ├── 4 - 256to1 4-bit multiplexer.v │ └── 3 - 9 to 1 Multiplexer.v ├── 4 - K Maps │ ├── 7 - K Map.v │ ├── 1 - Three Variable.v │ ├── 6 - K Map with dont care terms.v │ ├── 4 - Four Variable.v │ ├── 3 - Four Variable - SOP.v │ ├── 2 - Four Variable-POS.v │ ├── 5 - Minimum SOp & POS.v │ └── 8 - K Map Implemented with a Multiplexer.v └── 3 - Arithmatic Circuits │ ├── 1 - Half adder.v │ ├── 2 - Full Adder.v │ ├── 3 - 3-bit binary adder.v │ ├── 4 - Adder.v │ ├── 6 - 4-digit BDC adder.v │ ├── 5 - 100-bit binray Adder.v │ └── 7 - Signed addition overflow.v ├── 2 - Vectors ├── 7 - Vector Reversal.v ├── 9 - More Replication.v ├── 5 - Four Input Gate.v ├── 8 - Replication Operator.v ├── 4 - Bitwise Operators.v ├── 3 - Vector part select.v ├── 1 - Vectors.v ├── 2 - Vectors in more detail.v └── 6 - Vector Concatanation Operator.v ├── 8 - Verification └── 1 - Finding Bugs in the Code │ ├── 2 - Nand gate fix.v │ ├── 1 - 2to1 Mux fix.v │ ├── 3 - 4 to 1 Mux fix.v │ ├── 4 - Adder subtractor fix.v │ └── 5 - Case Statement fix.v ├── 5 - More Verilog Features ├── 2 - reduction operators.v ├── 3 - reduction - even wider gates.v ├── 4 - combinational for loop - vector reversal.v ├── 1 - Conditional Ternary Operator.v ├── 5 - Combinational for loop- 255 bit population count.v ├── 7 - Generate for loops - 100 digit BCD Adder.v └── 6 - Generate for loop - 100-bit Binary Adder.v ├── 7 - Sequential Logic ├── 1 - Latches & Flipflops │ ├── 2 - 8 D Flipflops.v │ ├── 10 - DFF+gate (XOR).v │ ├── 1 - D Flipflop.v │ ├── 9 - DFF2.v │ ├── 8 - DFF.v │ ├── 15 - Detect an edge.v │ ├── 16 - Detect both edges.v │ ├── 11 - MUX and DFF.v │ ├── 4 - DFF with reset value (negedge).v │ ├── 17 - Double edge triggered flip flop.v │ ├── 3 - DFF With Reset.v │ ├── 12 - MUX and DFF2.v │ ├── 5 - DFF with Asynchronus reset.v │ ├── 7 - D Latch.v │ └── 6 - DFF with byte enable.v └── 2 - Counters │ ├── 1 - Four-bit binary counter.v │ ├── 3 - Decade counter (1 to 10).v │ ├── 2 - Decade counter.v │ └── 4 - Slow decade counter.v ├── README.md ├── 4 - Procedures ├── 6 - Priority Encorder - LSB.v ├── 4 - If statement Latches.v ├── 3 - If Statement.v ├── 5 - Case Statement.v ├── 8 -Avoiding Latches - Alternative way.v ├── 2 - Always Block_clocked.v ├── 7 - Priority encorder with casez.v └── 1 - always_blocks_combinational.v └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.txt 2 | -------------------------------------------------------------------------------- /1 - Basics/03 - Simple Wire.v: -------------------------------------------------------------------------------- 1 | module top_module( input in, output out ); 2 | assign out = in; 3 | endmodule 4 | -------------------------------------------------------------------------------- /3 - Modules/1 - Modules.v: -------------------------------------------------------------------------------- 1 | module top_module ( input a, input b, output out ); 2 | mod_a instance_1(a,b,out); 3 | endmodule 4 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/2 - GND.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | output out); 3 | assign out = 1'b0; 4 | endmodule 5 | -------------------------------------------------------------------------------- /1 - Basics/02 - Output Zero.v: -------------------------------------------------------------------------------- 1 | module top_module(output zero);// Module body starts after semicolon 2 | assign zero = 0; 3 | endmodule 4 | -------------------------------------------------------------------------------- /1 - Basics/05 - Not Gate.v: -------------------------------------------------------------------------------- 1 | module top_module( input in, output out ); 2 | assign out = ~in;//~ : bitwise negation operator 3 | endmodule 4 | -------------------------------------------------------------------------------- /1 - Basics/08 - XNOR Gate.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | output out ); 5 | assign out = ~(a^b); 6 | endmodule 7 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/1- Wire.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input in, 3 | output out); 4 | assign out = in; 5 | 6 | endmodule 7 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/10 - Simple circuit A.v: -------------------------------------------------------------------------------- 1 | module top_module (input x, input y, output z); 2 | assign z = (x^y) & x; 3 | endmodule 4 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/11 - Simple circuit B.v: -------------------------------------------------------------------------------- 1 | module top_module ( input x, input y, output z ); 2 | assign z= ~(x^y); 3 | endmodule 4 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/3 - NOR.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input in1, 3 | input in2, 4 | output out); 5 | assign out = ~(in1|in2); 6 | endmodule 7 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/9 - Two-bit equality.v: -------------------------------------------------------------------------------- 1 | module top_module ( input [1:0] A, input [1:0] B, output z ); 2 | assign z = (A==B); 3 | 4 | endmodule 5 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/4 - Another gate.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input in1, 3 | input in2, 4 | output out); 5 | assign out = in1&&~in2; 6 | endmodule 7 | -------------------------------------------------------------------------------- /6 - Combinational Logic/2 - Multiplexers/1 - 2 to 1 Multiplexer.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, b, sel, 3 | output out ); 4 | 5 | assign out = sel?b:a; 6 | endmodule 7 | -------------------------------------------------------------------------------- /6 - Combinational Logic/4 - K Maps/7 - K Map.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [4:1] x, 3 | output f 4 | ); 5 | assign f = (~x[1]&x[3])|(~x[2]&~x[4])|(x[2]&x[3]&x[4]); 6 | endmodule -------------------------------------------------------------------------------- /1 - Basics/06 - And Gate.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | output out ); 5 | 6 | assign out = a & b; // & : Bit-wise and operator 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /2 - Vectors/7 - Vector Reversal.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [7:0] in, 3 | output [7:0] out 4 | ); 5 | assign out = {in[0],in[1],in[2],in[3],in[4],in[5],in[6],in[7]}; 6 | endmodule 7 | -------------------------------------------------------------------------------- /1 - Basics/01 - Getting Started.v: -------------------------------------------------------------------------------- 1 | module top_module( output one ); 2 | 3 | // Insert your code here 4 | assign one = 1; //assign allows to drive an output continuously with a value 5 | 6 | endmodule 7 | -------------------------------------------------------------------------------- /1 - Basics/07 - Nor Gate.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | output out ); 5 | assign out = ~(a|b); //NOR gate using bitwise OR and NOT operators 6 | 7 | endmodule 8 | -------------------------------------------------------------------------------- /6 - Combinational Logic/2 - Multiplexers/2 - 2 to 1 Bus multiplexer.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [99:0] a, b, 3 | input sel, 4 | output [99:0] out ); 5 | assign out = sel?b:a; 6 | endmodule 7 | -------------------------------------------------------------------------------- /6 - Combinational Logic/4 - K Maps/1 - Three Variable.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | input c, 5 | output out ); 6 | assign out = a&(~b)&(~c) | c | b&(~c); 7 | endmodule -------------------------------------------------------------------------------- /6 - Combinational Logic/4 - K Maps/6 - K Map with dont care terms.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [4:1] x, 3 | output f ); 4 | 5 | assign f = (x[2]&x[4])|(x[3]&x[4])|(x[3]&~x[1]); 6 | endmodule 7 | -------------------------------------------------------------------------------- /6 - Combinational Logic/3 - Arithmatic Circuits/1 - Half adder.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, b, 3 | output cout, sum ); 4 | 5 | assign sum = a^b; 6 | assign cout = a&b; 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /6 - Combinational Logic/3 - Arithmatic Circuits/2 - Full Adder.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, b, cin, 3 | output cout, sum ); 4 | 5 | assign cout = a&b | b&cin | a&cin; 6 | assign sum = a^b^cin; 7 | endmodule -------------------------------------------------------------------------------- /6 - Combinational Logic/4 - K Maps/4 - Four Variable.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output out ); 7 | 8 | assign out = a^b^c^d; 9 | 10 | endmodule -------------------------------------------------------------------------------- /1 - Basics/04 - Four Wires.v: -------------------------------------------------------------------------------- 1 | module top_module( input a,b,c,output w,x,y,z ); 2 | 3 | assign w = a; //assign statements are executed in parallel 4 | assign x = b; 5 | assign y = b; 6 | assign z = c; 7 | endmodule 8 | -------------------------------------------------------------------------------- /6 - Combinational Logic/4 - K Maps/3 - Four Variable - SOP.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output out ); 7 | 8 | assign out = a|(~a&~b&c); 9 | endmodule 10 | -------------------------------------------------------------------------------- /8 - Verification/1 - Finding Bugs in the Code/2 - Nand gate fix.v: -------------------------------------------------------------------------------- 1 | module top_module (input a, input b, input c, output out);// 2 | wire con; 3 | andgate inst1 ( con, a, b, c, 1'b1, 1'b1); 4 | assign out = ~con; 5 | 6 | endmodule 7 | -------------------------------------------------------------------------------- /5 - More Verilog Features/2 - reduction operators.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] in, 3 | output parity); 4 | //even parity is calculated by XOR ing all the data bits 5 | 6 | assign parity = ^in; 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /7 - Sequential Logic/1 - Latches & Flipflops/2 - 8 D Flipflops.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input [7:0] d, 4 | output [7:0] q 5 | ); 6 | always @ (posedge clk) begin 7 | q <= d; 8 | end 9 | endmodule 10 | -------------------------------------------------------------------------------- /7 - Sequential Logic/1 - Latches & Flipflops/10 - DFF+gate (XOR).v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input in, 4 | output out); 5 | 6 | always @ (posedge clk) begin 7 | out <= out ^ in; 8 | end 9 | endmodule 10 | -------------------------------------------------------------------------------- /8 - Verification/1 - Finding Bugs in the Code/1 - 2to1 Mux fix.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input sel, 3 | input [7:0] a, 4 | input [7:0] b, 5 | output [7:0] out ); 6 | 7 | assign out = ({8{sel}} & a) | ({8{~sel}} & b); 8 | 9 | endmodule -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/5 - Two Gates.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input in1, 3 | input in2, 4 | input in3, 5 | output out); 6 | 7 | wire wire1; 8 | assign wire1 = ~(in1^in2); 9 | assign out = wire1^in3; 10 | endmodule 11 | -------------------------------------------------------------------------------- /3 - Modules/2 - Connect ports by position.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output out1, 7 | output out2 8 | ); 9 | //connect module by position 10 | mod_a inst_1(out1,out2,a,b,c,d); 11 | 12 | endmodule -------------------------------------------------------------------------------- /3 - Modules/4 - Connecting Three Modules.v: -------------------------------------------------------------------------------- 1 | module top_module ( input clk, input d, output q ); 2 | wire con1,con2; 3 | 4 | my_dff d_flop1(.clk(clk),.d(d),.q(con1)); 5 | my_dff d_flop2(.clk(clk),.d(con1),.q(con2)); 6 | my_dff d_flop3(.clk(clk),.d(con2),.q(q)); 7 | endmodule -------------------------------------------------------------------------------- /5 - More Verilog Features/3 - reduction - even wider gates.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [99:0] in, 3 | output out_and, 4 | output out_or, 5 | output out_xor 6 | ); 7 | assign out_and = ∈ 8 | assign out_or = |in; 9 | assign out_xor = ^in; 10 | endmodule -------------------------------------------------------------------------------- /7 - Sequential Logic/1 - Latches & Flipflops/1 - D Flipflop.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, // Clocks are used in sequential circuits 3 | input d, 4 | output reg q );// 5 | 6 | always @ (posedge clk) begin 7 | q <= d; 8 | 9 | end 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /7 - Sequential Logic/1 - Latches & Flipflops/9 - DFF2.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input d, 4 | input r, // synchronous reset 5 | output q); 6 | always @ (posedge clk) begin 7 | if (r) q <= 1'b0; 8 | else q <= d; 9 | end 10 | endmodule -------------------------------------------------------------------------------- /3 - Modules/6 - Adder1.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [31:0] a, 3 | input [31:0] b, 4 | output [31:0] sum 5 | ); 6 | wire con1, con2; 7 | add16 adder_1(a[15:0], b[15:0], 0, sum[15:0], con1); 8 | add16 adder_2(a[31:16], b[31:16], con1, sum[31:16], con2); 9 | 10 | endmodule -------------------------------------------------------------------------------- /6 - Combinational Logic/4 - K Maps/2 - Four Variable-POS.v: -------------------------------------------------------------------------------- 1 | //Here product of sum is used. 2 | 3 | module top_module( 4 | input a, 5 | input b, 6 | input c, 7 | input d, 8 | output out ); 9 | 10 | assign out = (c|!d|!b) & (!a|!b|c) & (a|b|!c|!d) & (!a|!c|d); 11 | endmodule 12 | -------------------------------------------------------------------------------- /6 - Combinational Logic/4 - K Maps/5 - Minimum SOp & POS.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output out_sop, 7 | output out_pos 8 | ); 9 | assign out_sop = (c&d)|(~a&~b&c); 10 | assign out_pos = c&(~b|~c|d)&(~a|~c|d); 11 | endmodule 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HDL-Bits-Solutions 2 | This is a repository containing solutions to the problem statements given in HDL Bits website. It has 180 problems covering various aspects of Digital designing such as Flipflops, Latches, Combinational circuits, FSMs etc. 3 | 4 | Link : https://hdlbits.01xz.net/wiki/Main_Page 5 | -------------------------------------------------------------------------------- /2 - Vectors/9 - More Replication.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, b, c, d, e, 3 | output [24:0] out );// 4 | 5 | // The output is XNOR of two vectors created by 6 | // concatenating and replicating the five inputs. 7 | assign out = ~{ {5{a}},{5{b}},{5{c}},{5{d}},{5{e}}} ^ { {5{a,b,c,d,e}}}; 8 | endmodule -------------------------------------------------------------------------------- /3 - Modules/3 - Connect by Name.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output out1, 7 | output out2 8 | ); 9 | //connect ports by name 10 | mod_a inst_1(.in1(a), .in2(b), .in3(c), .in4(d), .out1(out1), .out2(out2)); 11 | 12 | endmodule 13 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/7 - 7420 chip.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input p1a, p1b, p1c, p1d, 3 | output p1y, 4 | input p2a, p2b, p2c, p2d, 5 | output p2y ); 6 | 7 | assign p1y = ~(p1a && p1b && p1c && p1d); 8 | assign p2y = ~(p2a && p2b && p2c && p2d); 9 | 10 | endmodule 11 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/8 - Truth table.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input x3, 3 | input x2, 4 | input x1, // three inputs 5 | output f // one output 6 | ); 7 | //here sum of products is used 8 | assign f = (~x3&x2&~x1) | (~x3&x2&x1) | (x3&~x2&x1) | (x3&x2&x1); 9 | endmodule 10 | -------------------------------------------------------------------------------- /7 - Sequential Logic/2 - Counters/1 - Four-bit binary counter.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, // Synchronous active-high reset 4 | output [3:0] q); 5 | 6 | always @ (posedge clk) begin 7 | if (reset) q <= 4'd0; 8 | else q <= q+4'd1; 9 | end 10 | endmodule -------------------------------------------------------------------------------- /5 - More Verilog Features/4 - combinational for loop - vector reversal.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [99:0] in, 3 | output [99:0] out 4 | ); 5 | integer i; 6 | always @ (in) begin 7 | for (i=0;i<100; i=i+1) begin 8 | out[99-i] = in[i]; 9 | end 10 | end 11 | endmodule -------------------------------------------------------------------------------- /7 - Sequential Logic/1 - Latches & Flipflops/8 - DFF.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input d, 4 | input ar, // asynchronous reset 5 | output q); 6 | 7 | always @ (posedge clk, posedge ar) begin 8 | if (ar) q<=1'b0; 9 | else q<=d; 10 | 11 | end 12 | endmodule 13 | -------------------------------------------------------------------------------- /6 - Combinational Logic/1- Basic Gates/13 - Ring or vibrate.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input ring, 3 | input vibrate_mode, 4 | output ringer, // Make sound 5 | output motor // Vibrate 6 | ); 7 | assign motor = vibrate_mode && ring; 8 | assign ringer = (~vibrate_mode) && ring; 9 | endmodule 10 | -------------------------------------------------------------------------------- /6 - Combinational Logic/4 - K Maps/8 - K Map Implemented with a Multiplexer.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output out_sop, 7 | output out_pos 8 | ); 9 | assign out_sop = (c&d)|(~a&~b&c); 10 | assign out_pos = c&(~b|~c|d)&(~a|~c|d); 11 | endmodule 12 | -------------------------------------------------------------------------------- /7 - Sequential Logic/2 - Counters/3 - Decade counter (1 to 10).v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | output [3:0] q); 5 | 6 | always @ (posedge clk) begin 7 | if (reset) q <= 4'd1; 8 | else if (q == 4'd10) q<= 4'd1; 9 | else q <= q+4'd1; 10 | end 11 | 12 | endmodule 13 | -------------------------------------------------------------------------------- /2 - Vectors/5 - Four Input Gate.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [3:0] in, 3 | output out_and, 4 | output out_or, 5 | output out_xor 6 | ); 7 | 8 | assign out_and = in[3] && in[2] && in[1] && in[0]; 9 | assign out_or = in[3] || in[2] || in[1] || in[0]; 10 | assign out_xor = in[3] ^ in[2] ^ in[1] ^ in[0]; 11 | 12 | endmodule -------------------------------------------------------------------------------- /7 - Sequential Logic/2 - Counters/2 - Decade counter.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, // Synchronous active-high reset 4 | output [3:0] q); 5 | 6 | always @ (posedge clk) begin 7 | if (reset) q <= 4'd0; 8 | else if (q == 4'd9) q<= 4'd0; 9 | else q <= q+4'd1; 10 | end 11 | endmodule -------------------------------------------------------------------------------- /7 - Sequential Logic/1 - Latches & Flipflops/15 - Detect an edge.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input [7:0] in, 4 | output [7:0] pedge 5 | ); 6 | reg [7:0]intermediate; 7 | 8 | always @ (posedge clk) begin 9 | intermediate <= in; 10 | pedge <= (~intermediate) & in; 11 | end 12 | 13 | endmodule 14 | -------------------------------------------------------------------------------- /7 - Sequential Logic/1 - Latches & Flipflops/16 - Detect both edges.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input [7:0] in, 4 | output [7:0] anyedge 5 | ); 6 | reg [7:0]intermediate; 7 | 8 | always @ (posedge clk) begin 9 | intermediate <= in; 10 | anyedge <= intermediate ^ in; 11 | end 12 | endmodule 13 | -------------------------------------------------------------------------------- /2 - Vectors/8 - Replication Operator.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] in, 3 | output [31:0] out );// 4 | // The replication operator allows repeating a vector and concatenating them together: 5 | //{num{vector}} 6 | 7 | //this is sign-extending a smaller number to a larger one 8 | assign out = { {24{in[7]}} , in[7:0] }; 9 | 10 | endmodule -------------------------------------------------------------------------------- /5 - More Verilog Features/1 - Conditional Ternary Operator.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] a, b, c, d, 3 | output [7:0] min); 4 | 5 | wire [7:0] int1, int2; 6 | 7 | //condition ? if_true : if_false 8 | assign int1 = (a 25 | -------------------------------------------------------------------------------- /8 - Verification/1 - Finding Bugs in the Code/5 - Case Statement fix.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] code, 3 | output reg [3:0] out, 4 | output reg valid );// 5 | 6 | always @(*) begin 7 | case (code) 8 | 8'h45:begin 9 | out = 0; 10 | valid = 1; 11 | end 12 | 8'h16: begin 13 | out = 1; 14 | valid = 1; 15 | end 16 | 8'h1e: begin 17 | out = 2; 18 | valid = 1; 19 | end 20 | 8'h26: begin 21 | out = 3; 22 | valid = 1; 23 | end 24 | 8'h25: begin 25 | out = 4; 26 | valid = 1; 27 | end 28 | 8'h2e: begin 29 | out = 5; 30 | valid = 1; 31 | end 32 | 8'h36: begin 33 | out = 6; 34 | valid = 1; 35 | end 36 | 8'h3d: begin 37 | out = 7; 38 | valid = 1; 39 | end 40 | 8'h3e: begin 41 | out = 8; 42 | valid = 1; 43 | end 44 | 8'h46: begin 45 | out = 9; 46 | valid = 1; 47 | end 48 | default: begin 49 | valid = 0; 50 | out = 0; 51 | end 52 | endcase 53 | end 54 | 55 | endmodule --------------------------------------------------------------------------------