├── 0. Getting Started ├── 01_StepOne.v └── 02_OutputZero.v ├── 1. Verilog Language ├── 1. Basics │ ├── 01_SimpleWire.v │ ├── 03_Inverter.v │ ├── 04_ANDgate.v │ ├── 05_NORgate.v │ ├── 06_XNORgate.v │ ├── 02_FourWire.v │ ├── 07_DeclaringWires.v │ └── 08_7458chip.v ├── 5. More Verilog Features │ ├── 02_ReductionOperators.v │ ├── 03_Reduction:EvenWiderGates.v │ ├── 01_ConditionalTernaryOperator.v │ ├── 04_CombinationalFor-Loop:VectorReversal2.v │ ├── 05_CombinationalFor-loop:255BitPopulationCount.v │ ├── 06_GenerateForLoop100bitBinaryAdder2.v │ └── 07_GenerateFor-loop:100digitBCDAdder.v ├── 2. Vectors │ ├── 08_ReplicationOperator.v │ ├── 07_VectorReversal1.v │ ├── 06_VectorConcatenationOperator.v │ ├── 02_VectorsInMoreDetail.v │ ├── 03_VectorPartSelect.v │ ├── 09_MoreReplication.v │ ├── 01_Vectors.v │ ├── 05_FourInputGates.v │ └── 04_BitwiseOperators.v ├── 3. Modules: Hierarchy │ ├── 01_Modules.v │ ├── 02_ConnectingPortsByPosition.v │ ├── 06_Adder1.v │ ├── 03_ConnectingPortsByName.v │ ├── 04_ThreeModules.v │ ├── 09_AdderSubtractor.v │ ├── 05_ModulesAndVectors.v │ ├── 07_Adder2.v │ └── 08_CarrySelectAdder.v └── 4. Procedures │ ├── 01_AlwaysBlocks(Combinational).v │ ├── 02_AlwaysBlocks(Clocked).v │ ├── 03_IfStatement.v │ ├── 08_AvoidingLatches.v │ ├── 07_PriorityEncoderWithCasez.v │ ├── 04_IfStatementLatches.v │ ├── 05_CaseStatement.v │ └── 06_PriorityEncoder.v ├── 2. Circuits ├── 1. Combinational Logic │ ├── 1. Basic Gates │ │ ├── 02_GND.v │ │ ├── 10_SimpleCircuitA.v │ │ ├── 11_SimpleCircuitB.v │ │ ├── 01_Wire.v │ │ ├── 12_CombineCircuitA&B.v │ │ ├── 03_NOR.v │ │ ├── 04_AnotherGate.v │ │ ├── 05_TwoGates.v │ │ ├── 09_TwoBit Equality.v │ │ ├── 08_TruthTables.v │ │ ├── 07_7420Chip.v │ │ ├── 13_RingOrVibrate.v │ │ ├── 15_3BitPopulationCount.v │ │ ├── 14_Thermostat.v │ │ ├── 17_EvenLongerVectors.v │ │ ├── 16_GatesAndVectors.v │ │ └── 06_MoreLogicGates.v │ ├── 2. Multiplexers │ │ ├── 01_2to1Multiplexer.v │ │ ├── 04_256to1Multiplexer.v │ │ ├── 02_2to1BusMultiplexer.v │ │ ├── 05_256to1-4bitMultiplexer.v │ │ └── 03_9to1Multiplexer.v │ ├── 3. Arithmetic Circuits │ │ ├── 04_Adder.v │ │ ├── 01_HalfAdder.v │ │ ├── 02_FullAdder.v │ │ ├── 06_100bitBinaryAdder.v │ │ ├── 05_SignedAdditionOverflow.v │ │ ├── 03_3bitBinaryAdder.v │ │ └── 07_4digitBCDAdder.v │ └── 4. Karnaugh Map to Circuit │ │ ├── 06_KarnaughMap1.v │ │ ├── 01_3Variable.v │ │ ├── 07_KarnaughMap2.v │ │ ├── 03_4VariableKmap2.v │ │ ├── 02_4VariableKmap1.v │ │ ├── 08_KmapWithMux.v │ │ ├── 05_MinimumSOP&POS.v │ │ └── 04_4VariableKmap3.v ├── 2. Sequential Logic │ ├── 1. Latches and Flip-Flops │ │ ├── 07_DLatch.v │ │ ├── 01_DFLipFlop.v │ │ ├── 02_DFlipFlops.v │ │ ├── 10_DFF+Gate.v │ │ ├── 09_DFF2.v │ │ ├── 08_DFF1.v │ │ ├── 11_MuxAndDFF1.v │ │ ├── 16_DetectBothEdges.v │ │ ├── 04_DFFwithResetValue.v │ │ ├── 03_DFFWithReset.v │ │ ├── 05_DFFwithAsynchronousReset.v │ │ ├── 12_MuxAndDFF2.v │ │ ├── 13_DFFsAndGates.v │ │ ├── 18_DualEdgeTrigerredFlipFlop.v │ │ ├── 14_CreateCircuitFromTruthTable.v │ │ ├── 17_EdgeCaptureRegister.v │ │ ├── 15_DetectAnEdge.v │ │ └── 06_DFFwithByteEnable.v │ ├── 2. Counters │ │ ├── 01_FourBitBinaryCounter.v │ │ ├── 02_DecadeCounter.v │ │ ├── 03_DecadeCounterAgain.v │ │ ├── 04_SlowDecadeCounter.v │ │ ├── 06_Counter1000.v │ │ ├── 05_Counter1-12.v │ │ ├── 07_4digitDecimalCounter.v │ │ └── 08_12-hourClock.v │ ├── 5. Finite State Machines │ │ ├── 28_Q6cOneHotNextStateLogic.v │ │ ├── 31_Q2b:OneHotFSMEquations.v │ │ ├── 06_SimpleOneHotStateTransition3.v │ │ ├── 05_SimpleStateTransition3.v │ │ ├── 23_Q5bSerialTwoComplementer(Mealy).v │ │ ├── 27_Q6b:FSMNextStateLogic.v │ │ ├── 03_SimpleFSM2(asycnhronousReset).v │ │ ├── 04_SimpleFSM2(synchronousReset).v │ │ ├── 26_Q3cFSMLogic.v │ │ ├── 02_SimpleFSM1(synchronousReset).v │ │ ├── 25_Q3bFSM.v │ │ ├── 08_SimpleFSM3(synchronousReset).v │ │ ├── 01_SimpleFSM1(asynchronousReset).v │ │ ├── 07_SimpleFSM3(asynchronousReset).v │ │ ├── 21_Q8DesignMealyFSM.v │ │ ├── 15_PS2PacketParser.v │ │ ├── 10_Lemmings1.v │ │ ├── 24_Q3aFSM.v │ │ ├── 14_OneHotFSM.v │ │ ├── 22_Q5aSerialTwoComplementer(Moore).v │ │ ├── 11_Lemmings2.v │ │ ├── 29_Q6:FSM.v │ │ ├── 30_Q2a:FSM.v │ │ ├── 16_PS2PacketParserWithDataPath.v │ │ ├── 20_SequenceRecognition.v │ │ ├── 17_SerialReciever.v │ │ ├── 32_Q2a:FSM.v │ │ ├── 09_DesignAMooreFSM.v │ │ ├── 18_SerialRecieverWithDataPath.v │ │ ├── 12_Lemmings3.v │ │ ├── 19_SerialRecieverWithParityChecking.v │ │ ├── 33_Q2b:AnotherFSM.v │ │ └── 13_Lemmings4.v │ ├── 3. Shift Registers │ │ ├── 04_5-BitLFSR.v │ │ ├── 06_32-BitLFSR.v │ │ ├── 02_LeftRightRotator.v │ │ ├── 05_3-BitLFSR.v │ │ ├── 07_ShiftRegister1.v │ │ ├── 01_4bitShiftRegister.v │ │ ├── 09_3-inputLUT.v │ │ ├── 08_ShiftRegister2.v │ │ └── 03_LeftRightArithematicShiftby1or8.v │ └── 4. More Circuits │ │ ├── 01_Rule90.v │ │ ├── 02_Rule110.v │ │ └── 03_ConwaysGameofLife16x16.v └── 3. Building Larger Circuits │ ├── 01_CounterWith1000Period.v │ ├── 02_4BitShiftRegisterAnd DownCounter.v │ ├── 04_FSM:EnableShiftRegister.v │ ├── 03_FSM:Sequence1101Recognizer.v │ ├── 07_OneHotLogicEquations.v │ ├── 05_FSM:TheCompleteFSM.v │ └── 06_TheCompleteTimer.v ├── 3. Verification : Reading Simulations ├── 1. Finding bugs in code │ ├── 02_NAND.v │ ├── 01_Mux.v │ ├── 03_Mux.v │ ├── 04_AddSub.v │ └── 05_CaseStatement.v └── 2. Build a circuit from a simulation waveform │ ├── 01_CombinationalCircuit1.v │ ├── 04_CombinationalCircuit4.v │ ├── 07_SequentialCircuit7.v │ ├── 03_CombinationalCircuit3.v │ ├── 02_CombinationalCircuit2.v │ ├── 08_SequentialCircuit8.v │ ├── 10_SequentialCircuit10.v │ ├── 09_SequentialCircuit9.v │ ├── 05_CombinationalCircuit5.v │ └── 06_CombinationalCircuit6.v ├── 4. Verification : Writing Testbenches ├── 01_Clock.v ├── 03_ANDGate.v ├── 02_Testbench1.v ├── 04_Testbench2.v └── 05_Tflip-flop.v ├── README.md ├── 5. CS450 ├── 01_Timer.v ├── 03_HistoryShift.v ├── 02_Counter2bc.v └── 04_GShare.v └── LICENSE /0. Getting Started/01_StepOne.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | output one 3 | ); 4 | 5 | assign one = 1; 6 | 7 | endmodule 8 | -------------------------------------------------------------------------------- /0. Getting Started/02_OutputZero.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | output zero 3 | ); 4 | 5 | assign zero = 0; 6 | 7 | endmodule 8 | -------------------------------------------------------------------------------- /1. Verilog Language/1. Basics/01_SimpleWire.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input in, 3 | output out ); 4 | 5 | assign out = in; 6 | 7 | endmodule 8 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/02_GND.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | output out 3 | ); 4 | 5 | assign out = 0; 6 | 7 | endmodule 8 | -------------------------------------------------------------------------------- /1. Verilog Language/1. Basics/03_Inverter.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input in, 3 | output out 4 | ); 5 | 6 | assign out = ~in; 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/10_SimpleCircuitA.v: -------------------------------------------------------------------------------- 1 | module top_module (input x, input y, output z); 2 | assign z = (x^y) & x; 3 | endmodule 4 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/11_SimpleCircuitB.v: -------------------------------------------------------------------------------- 1 | module top_module ( input x, input y, output z ); 2 | assign z = ~(x ^ y); 3 | endmodule 4 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/01_Wire.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input in, 3 | output out 4 | ); 5 | 6 | assign out = in; 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /1. Verilog Language/1. Basics/04_ANDgate.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | output out 5 | ); 6 | 7 | assign out = a & b; 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /1. Verilog Language/1. Basics/05_NORgate.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | output out 5 | ); 6 | 7 | assign out = ~(a | b); 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /1. Verilog Language/1. Basics/06_XNORgate.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | output out 5 | ); 6 | 7 | assign out = ~(a ^ b); 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /1. Verilog Language/5. More Verilog Features/02_ReductionOperators.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] in, 3 | output parity 4 | ); 5 | 6 | assign parity = ^in; 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/2. Multiplexers/01_2to1Multiplexer.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, b, sel, 3 | output out 4 | ); 5 | 6 | assign out = sel ? b : a; 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /1. Verilog Language/2. Vectors/08_ReplicationOperator.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] in, 3 | output [31:0] out 4 | ); 5 | 6 | assign out = { { 24{in[7] } } , in[7:0] }; 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/12_CombineCircuitA&B.v: -------------------------------------------------------------------------------- 1 | module top_module (input x, input y, output z); 2 | assign z = (((x ^ y) & x) | (~(x ^ y))) ^ (((x ^ y) & x) & (~(x ^ y))); 3 | endmodule 4 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/03_NOR.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input in1, 3 | input in2, 4 | output out 5 | ); 6 | 7 | assign out = ~(in1 | in2); 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/04_AnotherGate.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input in1, 3 | input in2, 4 | output out 5 | ); 6 | 7 | assign out = in1 & ~in2; 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /1. Verilog Language/1. Basics/02_FourWire.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a,b,c, 3 | output w,x,y,z ); 4 | 5 | assign w = a; 6 | assign x = b; 7 | assign y = b; 8 | assign z = c; 9 | 10 | endmodule 11 | -------------------------------------------------------------------------------- /1. Verilog Language/2. Vectors/07_VectorReversal1.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] in, 3 | output [7:0] out 4 | ); 5 | 6 | assign {out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7]} = in; 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/3. Arithmetic Circuits/04_Adder.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [3:0] x, 3 | input [3:0] y, 4 | output [4:0] sum 5 | ); 6 | 7 | assign sum = x + y; 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/4. Karnaugh Map to Circuit/06_KarnaughMap1.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [4:1] x, 3 | output f 4 | ); 5 | 6 | assign f = (x[3] & ~x[1]) | (~x[3] & x[1] & x[2]); 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /1. Verilog Language/2. Vectors/06_VectorConcatenationOperator.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [4:0] a, b, c, d, e, f, 3 | output [7:0] w, x, y, z 4 | ); 5 | 6 | assign {w,x,y,z} = {a,b,c,d,e,f,2'b11}; 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/3. Arithmetic Circuits/01_HalfAdder.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, b, 3 | output cout, sum 4 | ); 5 | 6 | assign sum = a ^ b; 7 | assign cout = a & b; 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/2. Multiplexers/04_256to1Multiplexer.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [255:0] in, 3 | input [7:0] sel, 4 | output out 5 | ); 6 | 7 | assign out = in[sel]; 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/4. Karnaugh Map to Circuit/01_3Variable.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | input c, 5 | output out 6 | ); 7 | 8 | assign out = a | b | c; 9 | 10 | endmodule 11 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/1. Finding bugs in code/02_NAND.v: -------------------------------------------------------------------------------- 1 | module top_module (input a, input b, input c, output out); 2 | wire out_t; 3 | assign out = ~out_t; 4 | andgate inst1 ( out_t ,a, b, c,1,1); 5 | 6 | endmodule 7 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/05_TwoGates.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input in1, 3 | input in2, 4 | input in3, 5 | output out 6 | ); 7 | 8 | assign out = in3 ^ ~(in1 ^ in2); 9 | 10 | endmodule 11 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/2. Multiplexers/02_2to1BusMultiplexer.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [99:0] a, b, 3 | input sel, 4 | output [99:0] out 5 | ); 6 | 7 | assign out = sel ? b : a; 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /1. Verilog Language/3. Modules: Hierarchy/01_Modules.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, 3 | input b, 4 | output out 5 | ); 6 | 7 | mod_a inst( 8 | .out(out), 9 | .in1(a), 10 | .in2(b) 11 | ); 12 | 13 | endmodule 14 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/4. Karnaugh Map to Circuit/07_KarnaughMap2.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [4:1] x, 3 | output f 4 | ); 5 | 6 | assign f = (x[3] & ~x[1]) | (x[2] & x[3] & x[4]) | (~x[4] & ~x[2]); 7 | 8 | endmodule 9 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/07_DLatch.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input d, 3 | input ena, 4 | output q 5 | ); 6 | 7 | always@(*)begin 8 | if(ena) 9 | q <= d; 10 | end 11 | endmodule 12 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/01_CombinationalCircuit1.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, 3 | input b, 4 | output q 5 | ); 6 | 7 | assign q = a & b;\ 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/09_TwoBit Equality.v: -------------------------------------------------------------------------------- 1 | module top_module ( input [1:0] A, input [1:0] B, output z ); 2 | always @(*) 3 | begin 4 | if(A == B) 5 | z = 1; 6 | else 7 | z = 0; 8 | end 9 | endmodule 10 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/01_DFLipFlop.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input d, 4 | output reg q 5 | ); 6 | 7 | always@(posedge clk)begin 8 | q <= d; 9 | end 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/02_DFlipFlops.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 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/2. Multiplexers/05_256to1-4bitMultiplexer.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [1023:0] in, 3 | input [7:0] sel, 4 | output [3:0] out 5 | ); 6 | 7 | assign out = in[4*sel + 3 -: 4]; 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/1. Finding bugs in code/01_Mux.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 | 8 | assign out = sel ? a : b ; 9 | 10 | endmodule 11 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/08_TruthTables.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input x3, 3 | input x2, 4 | input x1, // three inputs 5 | output f // one output 6 | ); 7 | 8 | assign f = x1&x3 | ~x3&x2; 9 | 10 | endmodule 11 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/3. Arithmetic Circuits/02_FullAdder.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, b, cin, 3 | output cout, sum 4 | ); 5 | 6 | assign sum = a ^ b ^ cin; 7 | assign cout = a & b | a & cin | b & cin; 8 | 9 | endmodule 10 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/4. Karnaugh Map to Circuit/03_4VariableKmap2.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output out 7 | ); 8 | 9 | assign out = a | (c & ~b); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/10_DFF+Gate.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input in, 4 | output out 5 | ); 6 | 7 | always@(posedge clk)begin 8 | out <= out ^ in; 9 | end 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/3. Arithmetic Circuits/06_100bitBinaryAdder.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [99:0] a, b, 3 | input cin, 4 | output cout, 5 | output [99:0] sum 6 | ); 7 | 8 | assign {cout,sum} = a + b + cin; 9 | 10 | endmodule 11 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/04_CombinationalCircuit4.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output q 7 | ); 8 | 9 | assign q = b | c; 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /4. Verification : Writing Testbenches/01_Clock.v: -------------------------------------------------------------------------------- 1 | `timescale 1ps / 1ps 2 | module top_module (); 3 | reg clk; 4 | initial clk = 0; 5 | always begin 6 | #5 clk = ~clk; 7 | end 8 | 9 | dut u_dut( 10 | .clk(clk) 11 | ); 12 | 13 | endmodule 14 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/07_SequentialCircuit7.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input a, 4 | output q 5 | ); 6 | 7 | always @(posedge clk) begin 8 | q <= ~a; 9 | end 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/4. Karnaugh Map to Circuit/02_4VariableKmap1.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output out 7 | ); 8 | 9 | assign out = (~b & ~c) | (~a & ~d) | (~a & b & c) | (a & c & d); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /1. Verilog Language/2. Vectors/02_VectorsInMoreDetail.v: -------------------------------------------------------------------------------- 1 | `default_nettype none 2 | module top_module( 3 | input wire [15:0] in, 4 | output wire [7:0] out_hi, 5 | output wire [7:0] out_lo 6 | ); 7 | 8 | assign out_hi = in[15:8]; 9 | assign out_lo = in[7:0]; 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /1. Verilog Language/2. Vectors/03_VectorPartSelect.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [31:0] in, 3 | output [31:0] out 4 | ); 5 | 6 | assign out[31:24] = in[7:0]; 7 | assign out[23:16] = in[15:8]; 8 | assign out[15:8] = in[23:16]; 9 | assign out[7:0] = in[31:24]; 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/03_CombinationalCircuit3.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output q 7 | ); 8 | 9 | assign q = (b & d) | (b & c) | (a & d) | (a & c); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /1. Verilog Language/5. More Verilog Features/03_Reduction:EvenWiderGates.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [99:0] in, 3 | output out_and, 4 | output out_or, 5 | output out_xor 6 | ); 7 | 8 | assign out_and = ∈ 9 | assign out_or = |in; 10 | assign out_xor = ^in; 11 | 12 | endmodule 13 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/09_DFF2.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input d, 4 | input r, 5 | output q 6 | ); 7 | always@(posedge clk)begin 8 | if(r) 9 | q <= 0; 10 | else 11 | q <= d; 12 | end 13 | endmodule 14 | -------------------------------------------------------------------------------- /1. Verilog Language/4. Procedures/01_AlwaysBlocks(Combinational).v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | output wire out_assign, 5 | output reg out_alwaysblock 6 | ); 7 | 8 | assign out_assign = a & b; 9 | 10 | always @(*) 11 | out_alwaysblock = a & b; 12 | 13 | endmodule 14 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/07_7420Chip.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 | 8 | assign p1y = ~(p1a & p1b & p1c & p1d); 9 | assign p2y = ~(p2a & p2b & p2c & p2d); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/4. Karnaugh Map to Circuit/08_KmapWithMux.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input c, 3 | input d, 4 | output [3:0] mux_in 5 | ); 6 | 7 | assign mux_in[0] = c | d; 8 | assign mux_in[1] = 0; 9 | assign mux_in[2] = ~d; 10 | assign mux_in[3] = c & d; 11 | 12 | endmodule 13 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/2. Counters/01_FourBitBinaryCounter.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | output [3:0] q 5 | ); 6 | 7 | always@(posedge clk)begin 8 | if(reset) 9 | q <= 0; 10 | else 11 | q <= q + 1'b1; 12 | end 13 | 14 | endmodule 15 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/5. Finite State Machines/28_Q6cOneHotNextStateLogic.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [6:1] y, 3 | input w, 4 | output Y2, 5 | output Y4 6 | ); 7 | 8 | assign Y2 = y[1] & (~w); 9 | assign Y4 = (y[2] & w) | (y[3] & w) | (y[5] & w) | (y[6] & w); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/3. Arithmetic Circuits/05_SignedAdditionOverflow.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] a, 3 | input [7:0] b, 4 | output [7:0] s, 5 | output overflow 6 | ); 7 | 8 | assign s = a + b; 9 | assign overflow = (~(a[7] ^ b[7])) & (s[7] != a[7]); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/2. Counters/02_DecadeCounter.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | output [3:0] q 5 | ); 6 | 7 | always@(posedge clk) begin 8 | if(reset | q==9) 9 | q <= 0; 10 | else 11 | q <= q + 1'b1; 12 | end 13 | 14 | endmodule 15 | -------------------------------------------------------------------------------- /1. Verilog Language/2. Vectors/09_MoreReplication.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, b, c, d, e, 3 | output [24:0] out 4 | ); 5 | 6 | wire [24:0] w1,w2; 7 | 8 | assign w1 = {{5{a}}, {5{b}}, {5{c}}, {5{d}}, {5{e}}}; 9 | assign w2 = { { 5{a,b,c,d,e} } }; 10 | assign out = ~(w1 ^ w2); 11 | 12 | endmodule 13 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/08_DFF1.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input d, 4 | input ar, 5 | output q 6 | ); 7 | 8 | always@(posedge clk, posedge ar)begin 9 | if(ar) 10 | q <= 0; 11 | else 12 | q <= d; 13 | end 14 | 15 | endmodule 16 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/11_MuxAndDFF1.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input L, 4 | input r_in, 5 | input q_in, 6 | output reg Q 7 | ); 8 | 9 | always @(posedge clk) begin 10 | if(L) 11 | Q <= r_in; 12 | else 13 | Q <= q_in; 14 | end 15 | endmodule 16 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/2. Counters/03_DecadeCounterAgain.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | output [3:0] q 5 | ); 6 | 7 | always@(posedge clk) begin 8 | if(reset | q==10) 9 | q <= 1'b1; 10 | else 11 | q <= q + 1'b1; 12 | end 13 | 14 | endmodule 15 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/5. Finite State Machines/31_Q2b:OneHotFSMEquations.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [5:0] y, 3 | input w, 4 | output Y1, 5 | output Y3 6 | ); 7 | 8 | assign Y1 = y[0] & w; 9 | assign Y3 = (y[1] & (~w)) | (y[2] & (~w)) | (y[4] & (~w)) | (y[5] & (~w)); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/02_CombinationalCircuit2.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output q 7 | ); 8 | 9 | assign q = (a + b + c + d == 0 | a + b + c + d == 2 | a + b + c + d == 4); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /1. Verilog Language/2. Vectors/01_Vectors.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input wire [2:0] vec, 3 | output wire [2:0] outv, 4 | output wire o2, 5 | output wire o1, 6 | output wire o0 7 | ); 8 | 9 | assign outv = vec; 10 | assign o0 = vec[0]; 11 | assign o1 = vec[1]; 12 | assign o2 = vec[2]; 13 | 14 | endmodule 15 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/13_RingOrVibrate.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input ring, 3 | input vibrate_mode, 4 | output ringer, // Make sound 5 | output motor // Vibrate 6 | ); 7 | assign ringer = ring & (~vibrate_mode); 8 | assign motor = ring & vibrate_mode; 9 | 10 | endmodule 11 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/16_DetectBothEdges.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input [7:0] in, 4 | output [7:0] anyedge 5 | ); 6 | 7 | reg [7:0] in_last; 8 | always @(posedge clk) begin 9 | in_last <= in; 10 | anyedge <= in ^ in_last; 11 | end 12 | 13 | endmodule 14 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/04_DFFwithResetValue.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | input [7:0] d, 5 | output [7:0] q 6 | ); 7 | always@(negedge clk)begin 8 | if(reset) 9 | q <= 8'h34; 10 | else 11 | q <= d; 12 | end 13 | endmodule 14 | -------------------------------------------------------------------------------- /1. Verilog Language/3. Modules: Hierarchy/02_ConnectingPortsByPosition.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 | 10 | mod_a inst( 11 | out1, 12 | out2, 13 | a, 14 | b, 15 | c, 16 | d 17 | ); 18 | 19 | endmodule 20 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/4. Karnaugh Map to Circuit/05_MinimumSOP&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 | 10 | assign out_sop = (c & d) | (~a & ~b & c); 11 | assign out_pos = (c) & (~a | b) & (d | ~b); 12 | 13 | endmodule 14 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/03_DFFWithReset.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | input [7:0] d, 5 | output [7:0] q 6 | ); 7 | 8 | always@(posedge clk)begin 9 | if(reset) 10 | q <= 0; 11 | else 12 | q <= d; 13 | end 14 | 15 | endmodule 16 | -------------------------------------------------------------------------------- /1. Verilog Language/5. More Verilog Features/01_ConditionalTernaryOperator.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] a, b, c, d, 3 | output [7:0] min 4 | ); 5 | 6 | wire [7:0] w1, w2, w3; 7 | 8 | assign w1 = (a < b) ? a : b; 9 | assign w2 = (c < w1) ? c : w1; 10 | assign w3 = (d < w2) ? d : w2; 11 | assign min = w3; 12 | 13 | endmodule 14 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/3. Shift Registers/04_5-BitLFSR.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input clk, 3 | input reset, 4 | output [4:0] q 5 | ); 6 | always @(posedge clk) begin 7 | if(reset) 8 | q <= 5'h1; 9 | else 10 | q <= {q[0]^1'b0, q[4], q[3]^q[0], q[2], q[1]}; 11 | end 12 | 13 | endmodule 14 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/4. Karnaugh Map to Circuit/04_4VariableKmap3.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | input c, 5 | input d, 6 | output out 7 | ); 8 | 9 | assign out = (~a&b&~c&~d) | (a&~b&~c&~d) | (~a&~b&~c&d) | (a&b&~c&d) | (~a&b&c&d) | (a&~b&c&d) | (~a&~b&c&~d) | (a&b&c&~d); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /1. Verilog Language/2. Vectors/05_FourInputGates.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 13 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/05_DFFwithAsynchronousReset.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input areset, 4 | input [7:0] d, 5 | output [7:0] q 6 | ); 7 | always@(posedge clk,posedge areset)begin 8 | if(areset) 9 | q <= 0; 10 | else 11 | q <= d; 12 | end 13 | 14 | endmodule 15 | -------------------------------------------------------------------------------- /1. Verilog Language/1. Basics/07_DeclaringWires.v: -------------------------------------------------------------------------------- 1 | `default_nettype none 2 | module top_module( 3 | input a, 4 | input b, 5 | input c, 6 | input d, 7 | output out, 8 | output out_n 9 | ); 10 | wire w1,w2; 11 | 12 | assign w1 = a & b; 13 | assign w2 = c & d; 14 | assign out = w1 | w2; 15 | assign out_n = ~out; 16 | 17 | endmodule 18 | -------------------------------------------------------------------------------- /1. Verilog Language/3. Modules: Hierarchy/06_Adder1.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [31:0] a, 3 | input [31:0] b, 4 | output [31:0] sum 5 | ); 6 | 7 | wire w1; 8 | reg x = 0; 9 | 10 | add16 inst0(.a(a[15:0]), .b(b[15:0]), .sum(sum[15:0]), .cin(x), .cout(w1)); 11 | add16 inst1(.a(a[31:16]), .b(b[31:16]), .sum(sum[31:16]), .cin(w1)); 12 | endmodule 13 | -------------------------------------------------------------------------------- /2. Circuits/3. Building Larger Circuits/01_CounterWith1000Period.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | output [9:0] q 5 | ); 6 | 7 | always @(posedge clk) begin 8 | if (reset) q <= 0; 9 | else begin 10 | if (q < 999) q <= q + 1'b1; 11 | else q <= 0; 12 | end 13 | end 14 | 15 | endmodule 16 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/15_3BitPopulationCount.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [2:0] in, 3 | output [1:0] out 4 | ); 5 | 6 | integer i; 7 | 8 | always @(*) begin 9 | out = 2'b0; 10 | for(i = 0; i < 3; i ++) begin 11 | if(in[i]) 12 | out = out + 2'b1; 13 | end 14 | end 15 | 16 | endmodule 17 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/12_MuxAndDFF2.v: -------------------------------------------------------------------------------- 1 | module top_module 2 | ( 3 | input clk, 4 | input w, R, E, L, 5 | output Q 6 | ); 7 | 8 | always @(posedge clk) begin 9 | if(L) Q <= R; 10 | else begin 11 | if(E) Q <= w; 12 | else Q <= Q; 13 | end 14 | end 15 | 16 | endmodule 17 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/4. More Circuits/01_Rule90.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input clk, 3 | input load, 4 | input [511:0] data, 5 | output [511:0] q 6 | ); 7 | 8 | always@(posedge clk) begin 9 | if(load) 10 | q <= data; 11 | else 12 | q <= {1'b0, q[511:1]} ^ {q[510:0], 1'b0}; 13 | end 14 | 15 | endmodule 16 | -------------------------------------------------------------------------------- /1. Verilog Language/3. Modules: Hierarchy/03_ConnectingPortsByName.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 | 10 | mod_a inst( 11 | .out1(out1), 12 | .out2(out2), 13 | .in1(a), 14 | .in2(b), 15 | .in3(c), 16 | .in4(d) 17 | ); 18 | 19 | endmodule 20 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/13_DFFsAndGates.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input x, 4 | output z 5 | ); 6 | 7 | reg [2:0] Q; 8 | always@(posedge clk)begin 9 | Q[0] <= Q[0] ^ x; 10 | Q[1] <= ~Q[1] & x; 11 | Q[2] <= ~Q[2] | x; 12 | end 13 | 14 | assign z = ~(| Q); 15 | 16 | endmodule 17 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/3. Shift Registers/06_32-BitLFSR.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input clk, 3 | input reset, 4 | output [31:0] q 5 | ); 6 | 7 | always @(posedge clk) begin 8 | if(reset) 9 | q <= 32'h1; 10 | else begin 11 | q <= {0^q[0], q[31:23], q[22]^q[0], q[21:3], q[2]^q[0], q[1]^q[0]}; 12 | end 13 | end 14 | endmodule 15 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/08_SequentialCircuit8.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clock, 3 | input a, 4 | output p, 5 | output q ); 6 | 7 | always @(*) begin 8 | if(clock) p = a; 9 | end 10 | 11 | always @(negedge clock) begin 12 | q <= p; 13 | end 14 | 15 | endmodule 16 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/10_SequentialCircuit10.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input a, 4 | input b, 5 | output q, 6 | output state 7 | ); 8 | 9 | always @ (posedge clk) begin 10 | if(a == b) 11 | state <= a; 12 | else 13 | state <= state; 14 | end 15 | assign q = (a == b) ? state : (~state); 16 | 17 | endmodule 18 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/14_Thermostat.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input too_cold, 3 | input too_hot, 4 | input mode, 5 | input fan_on, 6 | output heater, 7 | output aircon, 8 | output fan 9 | ); 10 | 11 | assign heater = mode & too_cold; 12 | assign aircon = (~mode) & too_hot; 13 | assign fan = fan_on | (heater | aircon); 14 | 15 | endmodule 16 | -------------------------------------------------------------------------------- /1. Verilog Language/2. Vectors/04_BitwiseOperators.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [2:0] a, 3 | input [2:0] b, 4 | output [2:0] out_or_bitwise, 5 | output out_or_logical, 6 | output [5:0] out_not 7 | ); 8 | 9 | assign out_or_bitwise = a | b; 10 | assign out_or_logical = a || b; 11 | assign out_not[5:3] = ~(b[2:0]); 12 | assign out_not[2:0] = ~(a[2:0]); 13 | 14 | endmodule 15 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/17_EvenLongerVectors.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [99:0] in, 3 | output [98:0] out_both, 4 | output [99:1] out_any, 5 | output [99:0] out_different 6 | ); 7 | 8 | assign out_both = in[98:0] & in[99:1]; 9 | assign out_any = in[98:0] | in[99:1]; 10 | assign out_different = {(in[99] ^ in[0]), in[98:0] ^ in[99:1]}; 11 | 12 | endmodule 13 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/2. Counters/04_SlowDecadeCounter.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input slowena, 4 | input reset, 5 | output [3:0] q 6 | ); 7 | 8 | always@(posedge clk)begin 9 | if(reset | (slowena & q==9)) 10 | q <= 0; 11 | else if(slowena) 12 | q <= q + 1'b1; 13 | else 14 | q <= q; 15 | end 16 | 17 | endmodule 18 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/09_SequentialCircuit9.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input a, 4 | output [3:0] q 5 | ); 6 | 7 | always @(posedge clk) begin 8 | if (a) q <= 4; 9 | else begin 10 | if (q < 6) q <= q + 1; 11 | else q <= 0; 12 | end 13 | end 14 | 15 | endmodule 16 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/18_DualEdgeTrigerredFlipFlop.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input d, 4 | output q 5 | ); 6 | 7 | reg [1:0] r; 8 | 9 | always @(posedge clk) begin 10 | r[0] <= d; 11 | end 12 | 13 | always @(negedge clk) begin 14 | r[1] <= d; 15 | end 16 | 17 | assign q = (clk) ? r[0] : r[1]; 18 | 19 | endmodule 20 | -------------------------------------------------------------------------------- /1. Verilog Language/5. More Verilog Features/04_CombinationalFor-Loop:VectorReversal2.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [99:0] in, 3 | output reg [99:0] out 4 | ); 5 | 6 | always @(*) begin 7 | for (int i=0 ; i < $bits(out) ; i++) // $bits() is a system function that returns the width of a signal. 8 | out[i] = in[$bits(out)-i-1]; // $bits(out) is 100 because out is 100 bits wide. 9 | end 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /1. Verilog Language/5. More Verilog Features/05_CombinationalFor-loop:255BitPopulationCount.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [254:0] in, 3 | output [7:0] out 4 | ); 5 | 6 | always @(*) begin 7 | out = 0; 8 | for(int i = 0; i < $bits(in); i++) begin 9 | if(in[i]) 10 | out = out + 8'b1; 11 | else 12 | out = out + 8'b0; 13 | end 14 | end 15 | 16 | endmodule 17 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/4. More Circuits/02_Rule110.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input clk, 3 | input load, 4 | input [511:0] data, 5 | output [511:0] q 6 | ); 7 | 8 | always @(posedge clk) begin 9 | if (load) begin 10 | q <= data; 11 | end 12 | else begin 13 | q <= (((q[511:0] ^ {q[510:0], 1'b0}) & q[511:1]) | ((q[511:0] | {q[510:0], 1'b0}) & (~q[511:1]))); 14 | end 15 | end 16 | endmodule 17 | -------------------------------------------------------------------------------- /4. Verification : Writing Testbenches/03_ANDGate.v: -------------------------------------------------------------------------------- 1 | `timescale 1ps / 1ps 2 | module top_module(); 3 | reg [1:0] in; 4 | wire out; 5 | 6 | initial begin 7 | in = 2'b00; 8 | #10; 9 | in = 2'b01; 10 | #10; 11 | in = 2'b10; 12 | #10; 13 | in = 2'b11; 14 | end 15 | andgate u_andgate( 16 | .in(in), 17 | .out(out) 18 | ); 19 | 20 | endmodule 21 | -------------------------------------------------------------------------------- /1. Verilog Language/1. Basics/08_7458chip.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input p1a, p1b, p1c, p1d, p1e, p1f, 3 | output p1y, 4 | input p2a, p2b, p2c, p2d, 5 | output p2y 6 | ); 7 | 8 | wire w1,w2,w3,w4; 9 | 10 | assign w1 = p2a & p2b; 11 | assign w2 = p2c & p2d; 12 | assign w3 = p1a & p1b & p1c; 13 | assign w4 = p1d & p1e & p1f; 14 | assign p1y = w3 | w4; 15 | assign p2y = w1 | w2; 16 | 17 | endmodule 18 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/14_CreateCircuitFromTruthTable.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input j, 4 | input k, 5 | output Q); 6 | 7 | always @(posedge clk) begin 8 | case({j, k}) 9 | 2'b00 : Q <= Q; 10 | 2'b01 : Q <= 0; 11 | 2'b10 : Q <= 1; 12 | 2'b11 : Q <= ~Q; 13 | endcase 14 | end 15 | 16 | endmodule 17 | -------------------------------------------------------------------------------- /1. Verilog Language/4. Procedures/02_AlwaysBlocks(Clocked).v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input clk, 3 | input a, 4 | input b, 5 | output wire out_assign, 6 | output reg out_always_comb, 7 | output reg out_always_ff 8 | ); 9 | 10 | assign out_assign = a ^ b; 11 | 12 | always@(*) 13 | out_always_comb = a ^ b; 14 | 15 | always@(posedge clk) 16 | out_always_ff = a ^ b; 17 | 18 | endmodule 19 | -------------------------------------------------------------------------------- /1. Verilog Language/4. Procedures/03_IfStatement.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, 3 | input b, 4 | input sel_b1, 5 | input sel_b2, 6 | output wire out_assign, 7 | output reg out_always 8 | ); 9 | 10 | assign out_assign = (sel_b1 && sel_b2) ? b : a; 11 | 12 | always @(*) begin 13 | if(sel_b1 && sel_b2) 14 | out_always = b; 15 | else 16 | out_always = a; 17 | end 18 | 19 | endmodule 20 | -------------------------------------------------------------------------------- /1. Verilog Language/3. Modules: Hierarchy/04_ThreeModules.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input d, 4 | output q 5 | ); 6 | 7 | wire w1,w2; 8 | 9 | my_dff inst1( 10 | .clk(clk), 11 | .d(d), 12 | .q(w1) 13 | ); 14 | 15 | my_dff inst2( 16 | .clk(clk), 17 | .d(w1), 18 | .q(w2) 19 | ); 20 | 21 | my_dff inst3( 22 | .clk(clk), 23 | .d(w2), 24 | .q(q) 25 | ); 26 | 27 | endmodule 28 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/17_EdgeCaptureRegister.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | input [31:0] in, 5 | output [31:0] out 6 | ); 7 | 8 | reg [31:0] in_last; 9 | 10 | always @(posedge clk) begin 11 | in_last <= in; 12 | if(reset) 13 | out <= 0; 14 | else begin 15 | out <= out | (in_last & ~in); 16 | end 17 | end 18 | 19 | endmodule 20 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/3. Shift Registers/02_LeftRightRotator.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input clk, 3 | input load, 4 | input [1:0] ena, 5 | input [99:0] data, 6 | output reg [99:0] q 7 | ); 8 | 9 | always@(posedge clk)begin 10 | if(load) q <= data; 11 | else begin 12 | if(ena == 2'b01) q <= {q[0], q[99:1]}; 13 | else if(ena == 2'b10) q<= {q[98:0], q[99]}; 14 | else q <= q; 15 | end 16 | end 17 | 18 | endmodule 19 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/1. Finding bugs in code/03_Mux.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [1:0] sel, 3 | input [7:0] a, 4 | input [7:0] b, 5 | input [7:0] c, 6 | input [7:0] d, 7 | output [7:0] out ); // 8 | 9 | wire [7:0] mux0; 10 | wire [7:0] mux1; 11 | 12 | mux2 u_mux2_1 ( sel[0], a, b, mux0 ); 13 | mux2 u_mux2_2 ( sel[0], c, d, mux1 ); 14 | mux2 u_mux2_3 ( sel[1], mux0, mux1, out ); 15 | 16 | endmodule 17 | -------------------------------------------------------------------------------- /4. Verification : Writing Testbenches/02_Testbench1.v: -------------------------------------------------------------------------------- 1 | `timescale 1ps / 1ps 2 | module top_module ( output reg A, output reg B ); 3 | 4 | // generate input patterns here 5 | initial begin 6 | A = 0; 7 | B = 0; 8 | #10; 9 | A = 1; 10 | B = 0; 11 | #5; 12 | A = 1; 13 | B = 1; 14 | #5; 15 | A = 0; 16 | B = 1; 17 | #20; 18 | A = 0; 19 | B = 0; 20 | end 21 | 22 | endmodule 23 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/1. Finding bugs in code/04_AddSub.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input do_sub, 3 | input [7:0] a, 4 | input [7:0] b, 5 | output reg [7:0] out, 6 | output reg result_is_zero 7 | ); 8 | 9 | always @(*) begin 10 | case (do_sub) 11 | 0: out = a+b; 12 | 1: out = a-b; 13 | endcase 14 | if (out == 0) 15 | result_is_zero = 1; 16 | else result_is_zero = 0; 17 | end 18 | 19 | endmodule 20 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/05_CombinationalCircuit5.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [3:0] a, 3 | input [3:0] b, 4 | input [3:0] c, 5 | input [3:0] d, 6 | input [3:0] e, 7 | output [3:0] q 8 | ); 9 | 10 | always @(*) begin 11 | case (c) 12 | 0 : q = b; 13 | 1 : q = e; 14 | 2 : q = a; 15 | 3 : q = d; 16 | default : q = 4'hf; 17 | endcase 18 | end 19 | 20 | endmodule 21 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/16_GatesAndVectors.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [3:0] in, 3 | output [2:0] out_both, 4 | output [3:1] out_any, 5 | output [3:0] out_different 6 | ); 7 | 8 | integer i; 9 | 10 | always @(*) 11 | begin 12 | out_different[3] = in[3] ^ in[0]; 13 | for(i=0; i<3; i++) 14 | begin 15 | out_both[i] = in[i] & in[i+1]; 16 | out_any[i+1] = in[i] | in[i+1]; 17 | out_different[i] = in[i] ^ in[i+1]; 18 | end 19 | end 20 | 21 | endmodule 22 | -------------------------------------------------------------------------------- /2. Circuits/3. Building Larger Circuits/02_4BitShiftRegisterAnd DownCounter.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input shift_ena, 4 | input count_ena, 5 | input data, 6 | output [3:0] q 7 | ); 8 | 9 | reg [3:0] shift_temp; 10 | 11 | always @(posedge clk) begin 12 | if (shift_ena) begin 13 | shift_temp <= {shift_temp[2:0], data}; 14 | end 15 | else if (count_ena) begin 16 | shift_temp <= shift_temp - 1'b1; 17 | end 18 | end 19 | 20 | assign q = shift_temp; 21 | 22 | endmodule 23 | -------------------------------------------------------------------------------- /1. Verilog Language/4. Procedures/08_AvoidingLatches.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [15:0] scancode, 3 | output reg left, 4 | output reg down, 5 | output reg right, 6 | output reg up 7 | ); 8 | 9 | always @(*) begin 10 | left = 0; down = 0; right = 0; up = 0; 11 | case(scancode) 12 | 16'he06b : left = 1; 13 | 16'he072 : down = 1; 14 | 16'he074 : right = 1; 15 | 16'he075 : up = 1; 16 | default : ; 17 | endcase 18 | end 19 | 20 | endmodule 21 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/3. Shift Registers/05_3-BitLFSR.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [2:0] SW, 3 | input [1:0] KEY, 4 | output [2:0] LEDR 5 | ); 6 | 7 | wire L; 8 | wire clk; 9 | wire [2:0] R; 10 | reg [2:0] Q; 11 | 12 | assign R = SW; 13 | assign clk = KEY[0]; 14 | assign L = KEY[1]; 15 | 16 | always @(posedge clk) begin 17 | if(L) 18 | Q <= R; 19 | else 20 | Q <= {Q[2]^Q[1], Q[0], Q[2]}; 21 | end 22 | 23 | assign LEDR = Q; 24 | 25 | endmodule 26 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/3. Shift Registers/07_ShiftRegister1.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input resetn, 4 | input in, 5 | output out 6 | ); 7 | 8 | reg [2:0] Q; 9 | 10 | always @(posedge clk) begin 11 | if(resetn) begin 12 | Q[0] <= in; 13 | Q[1] <= Q[0]; 14 | Q[2] <= Q[1]; 15 | out <= Q[2]; 16 | end 17 | else begin 18 | Q <= 3'b0; 19 | out <= 1'b0; 20 | end 21 | end 22 | 23 | endmodule 24 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/1. Basic Gates/06_MoreLogicGates.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input a, b, 3 | output out_and, 4 | output out_or, 5 | output out_xor, 6 | output out_nand, 7 | output out_nor, 8 | output out_xnor, 9 | output out_anotb 10 | ); 11 | 12 | assign out_and = a & b; 13 | assign out_or = a | b; 14 | assign out_xor = a ^ b; 15 | assign out_nand = ~(a & b); 16 | assign out_nor = ~(a | b); 17 | assign out_xnor = ~(a ^ b); 18 | assign out_anotb = a & ~b; 19 | 20 | endmodule 21 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/2. Counters/06_Counter1000.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | output OneHertz, 5 | output [2:0] c_enable 6 | ); 7 | 8 | wire [3:0] q0, q1, q2; 9 | 10 | bcdcount counter0 (clk, reset, c_enable[0], q0); 11 | bcdcount counter1 (clk, reset, c_enable[1], q1); 12 | bcdcount counter2 (clk, reset, c_enable[2], q2); 13 | 14 | assign c_enable = {(q1 == 4'd9) & (q0 == 4'd9), q0 == 4'd9, 1'b1}; 15 | assign OneHertz = (q2 == 4'd9) & (q1 == 4'd9) & (q0 == 4'd9); 16 | 17 | endmodule 18 | -------------------------------------------------------------------------------- /1. Verilog Language/3. Modules: Hierarchy/09_AdderSubtractor.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [31:0] a, 3 | input [31:0] b, 4 | input sub, 5 | output [31:0] sum 6 | ); 7 | 8 | wire cout1, cout2; 9 | wire [15:0] sum1, sum2; 10 | wire [31:0] b_xor; 11 | 12 | assign b_xor = sub ? ~b : b; 13 | assign sum = {sum2, sum1}; 14 | 15 | add16 instance1(.a(a[15:0]), .b(b_xor[15:0]), .cin(sub), .cout(cout1), .sum(sum1)); 16 | add16 instance2(.a(a[31:16]), .b(b_xor[31:16]), .cin(cout1), .cout(cout2), .sum(sum2)); 17 | 18 | endmodule 19 | -------------------------------------------------------------------------------- /3. Verification : Reading Simulations/2. Build a circuit from a simulation waveform/06_CombinationalCircuit6.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [2:0] a, 3 | output reg [15:0] q 4 | ); 5 | 6 | always@(*)begin 7 | case(a) 8 | 0 : q = 16'h1232; 9 | 1 : q = 16'haee0; 10 | 2 : q = 16'h27d4; 11 | 3 : q = 16'h5a0e; 12 | 4 : q = 16'h2066; 13 | 5 : q = 16'h64ce; 14 | 6 : q = 16'hc526; 15 | 7 : q = 16'h2f19; 16 | endcase 17 | end 18 | 19 | endmodule 20 | -------------------------------------------------------------------------------- /1. Verilog Language/5. More Verilog Features/06_GenerateForLoop100bitBinaryAdder2.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [99:0] a, b, 3 | input cin, 4 | output [99:0] cout, 5 | output [99:0] sum 6 | ); 7 | 8 | genvar i; 9 | 10 | assign sum[0] = a[0]^b[0]^cin; 11 | assign cout[0]=a[0]&b[0] | a[0]&cin | b[0]&cin; 12 | 13 | generate 14 | for(i=1; i<100 ; i=i+1)begin:FA 15 | assign sum[i] = a[i]^b[i]^cout[i-1]; 16 | assign cout[i] = a[i]&b[i] | a[i]&cout[i-1] | b[i]&cout[i-1]; 17 | end 18 | endgenerate 19 | 20 | endmodule 21 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/15_DetectAnEdge.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input [7:0] in, 4 | output [7:0] pedge 5 | ); 6 | 7 | integer i; 8 | reg [7:0] in_tmp; 9 | always @(posedge clk) begin 10 | for(i = 0; i < 8; i = i + 1) begin 11 | if(in_tmp[i] != in[i] && in[i] == 1) begin 12 | pedge[i] = 1; 13 | end 14 | else begin 15 | pedge[i] = 0; 16 | end 17 | in_tmp[i] = in[i]; 18 | end 19 | end 20 | 21 | endmodule 22 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/3. Shift Registers/01_4bitShiftRegister.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input clk, 3 | input areset, 4 | input load, 5 | input ena, 6 | input [3:0] data, 7 | output reg [3:0] q 8 | ); 9 | 10 | always@(posedge clk,posedge areset)begin 11 | if(areset) 12 | q <= 0; 13 | else begin 14 | if(load) 15 | q <= data; 16 | else if(ena) 17 | q <= {1'b0,q[3:1]}; 18 | else 19 | q <= q; 20 | end 21 | end 22 | 23 | endmodule 24 | -------------------------------------------------------------------------------- /1. Verilog Language/3. Modules: Hierarchy/05_ModulesAndVectors.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input [7:0] d, 4 | input [1:0] sel, 5 | output reg [7:0] q 6 | ); 7 | 8 | wire [7:0] w1,w2,w3; 9 | 10 | my_dff8(.clk(clk), .d(d), .q(w1)); 11 | my_dff8(.clk(clk), .d(w1), .q(w2)); 12 | my_dff8(.clk(clk), .d(w2), .q(w3)); 13 | 14 | always@(*)begin 15 | case(sel) 16 | 2'b00 : q <= d; 17 | 2'b01 : q <= w1; 18 | 2'b10 : q <= w2; 19 | 2'b11 : q <= w3; 20 | endcase 21 | end 22 | 23 | endmodule 24 | -------------------------------------------------------------------------------- /1. Verilog Language/4. Procedures/07_PriorityEncoderWithCasez.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [7:0] in, 3 | output reg [2:0] pos 4 | ); 5 | 6 | always @(*) begin 7 | casez(in) 8 | 8'bzzzzzzz1 : pos = 0; 9 | 8'bzzzzzz1z : pos = 1; 10 | 8'bzzzzz1zz : pos = 2; 11 | 8'bzzzz1zzz : pos = 3; 12 | 8'bzzz1zzzz : pos = 4; 13 | 8'bzz1zzzzz : pos = 5; 14 | 8'bz1zzzzzz : pos = 6; 15 | 8'b1zzzzzzz : pos = 7; 16 | default : pos = 0; 17 | endcase 18 | end 19 | 20 | endmodule 21 | -------------------------------------------------------------------------------- /1. Verilog Language/4. Procedures/04_IfStatementLatches.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input cpu_overheated, 3 | output reg shut_off_computer, 4 | input arrived, 5 | input gas_tank_empty, 6 | output reg keep_driving 7 | ); 8 | 9 | always @(*) begin 10 | if (cpu_overheated) 11 | shut_off_computer = 1; 12 | else 13 | shut_off_computer = 0; 14 | end 15 | 16 | always @(*) begin 17 | if (~arrived) 18 | keep_driving = ~gas_tank_empty; 19 | else 20 | keep_driving = 0; 21 | end 22 | 23 | endmodule 24 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/3. Arithmetic Circuits/03_3bitBinaryAdder.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [2:0] a, b, 3 | input cin, 4 | output [2:0] cout, 5 | output [2:0] sum 6 | ); 7 | 8 | integer i; 9 | assign sum[0] = a[0] ^ b[0] ^ cin; 10 | assign cout[0] = (a[0] & b[0]) | (a[0] & cin) | (b[0] & cin); 11 | always@(*) 12 | begin 13 | for(i=1; i<3; i++) 14 | begin 15 | sum[i] = a[i] ^ b[i] ^ cout[i-1]; 16 | cout[i] = (a[i] & b[i]) | (a[i] & cout[i-1]) | (b[i] & cout[i-1]); 17 | end 18 | end 19 | 20 | endmodule 21 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/3. Shift Registers/09_3-inputLUT.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input enable, 4 | input S, 5 | input A, B, C, 6 | output Z ); 7 | 8 | reg[7:0] q; 9 | 10 | always @(posedge clk) begin 11 | if(enable) 12 | q <= {q[6:0], S}; 13 | else 14 | q <= q; 15 | end 16 | 17 | always @(*) begin 18 | case({A, B, C}) 19 | 3'b000 : Z = q[0]; 20 | 3'b001 : Z = q[1]; 21 | 3'b010 : Z = q[2]; 22 | 3'b011 : Z = q[3]; 23 | 3'b100 : Z = q[4]; 24 | 3'b101 : Z = q[5]; 25 | 3'b110 : Z = q[6]; 26 | 3'b111 : Z = q[7]; 27 | endcase 28 | end 29 | 30 | endmodule 31 | -------------------------------------------------------------------------------- /2. Circuits/1. Combinational Logic/2. Multiplexers/03_9to1Multiplexer.v: -------------------------------------------------------------------------------- 1 | module top_module( 2 | input [15:0] a, b, c, d, e, f, g, h, i, 3 | input [3:0] sel, 4 | output [15:0] out 5 | ); 6 | 7 | always@(*)begin 8 | case(sel) 9 | 4'h0 : out = a; 10 | 4'h1 : out = b; 11 | 4'h2 : out = c; 12 | 4'h3 : out = d; 13 | 4'h4 : out = e; 14 | 4'h5 : out = f; 15 | 4'h6 : out = g; 16 | 4'h7 : out = h; 17 | 4'h8 : out = i; 18 | default : out = 16'hFFFF; 19 | endcase 20 | end 21 | 22 | endmodule 23 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/1. Latches and Flip-Flops/06_DFFwithByteEnable.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input resetn, 4 | input [1:0] byteena, 5 | input [15:0] d, 6 | output [15:0] q 7 | ); 8 | always@(posedge clk)begin 9 | if(!resetn) 10 | q <= 0; 11 | else begin 12 | if(byteena[0]) 13 | q[7:0] <= d[7:0]; 14 | else 15 | q[7:0] <= q[7:0]; 16 | if(byteena[1]) 17 | q[15:8] <= d[15:8]; 18 | else 19 | q[15:8] <= q[15:8]; 20 | end 21 | end 22 | endmodule 23 | -------------------------------------------------------------------------------- /1. Verilog Language/4. Procedures/05_CaseStatement.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [2:0] sel, 3 | input [3:0] data0, 4 | input [3:0] data1, 5 | input [3:0] data2, 6 | input [3:0] data3, 7 | input [3:0] data4, 8 | input [3:0] data5, 9 | output reg [3:0] out 10 | ); 11 | 12 | always@(*) begin 13 | case(sel) 14 | 0 : out = data0; 15 | 1 : out = data1; 16 | 2 : out = data2; 17 | 3 : out = data3; 18 | 4 : out = data4; 19 | 5 : out = data5; 20 | default : out = 0; 21 | endcase 22 | end 23 | 24 | endmodule 25 | -------------------------------------------------------------------------------- /1. Verilog Language/4. Procedures/06_PriorityEncoder.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input [3:0] in, 3 | output reg [1:0] pos 4 | ); 5 | 6 | always @(*) begin 7 | case (in) 8 | 4'h0: pos = 2'h0; 9 | 4'h1: pos = 2'h0; 10 | 4'h2: pos = 2'h1; 11 | 4'h3: pos = 2'h0; 12 | 4'h4: pos = 2'h2; 13 | 4'h5: pos = 2'h0; 14 | 4'h6: pos = 2'h1; 15 | 4'h7: pos = 2'h0; 16 | 4'h8: pos = 2'h3; 17 | 4'h9: pos = 2'h0; 18 | 4'ha: pos = 2'h1; 19 | 4'hb: pos = 2'h0; 20 | 4'hc: pos = 2'h2; 21 | 4'hd: pos = 2'h0; 22 | 4'he: pos = 2'h1; 23 | 4'hf: pos = 2'h0; 24 | default: pos = 2'b0; 25 | endcase 26 | end 27 | 28 | endmodule 29 | -------------------------------------------------------------------------------- /2. Circuits/2. Sequential Logic/2. Counters/05_Counter1-12.v: -------------------------------------------------------------------------------- 1 | module top_module ( 2 | input clk, 3 | input reset, 4 | input enable, 5 | output [3:0] Q, 6 | output c_enable, 7 | output c_load, 8 | output [3:0] c_d 9 | ); 10 | 11 | initial Q <= 1; 12 | 13 | always @(posedge clk) begin 14 | if(reset | ((Q == 12) & enable)) Q <= 1; 15 | else Q <= (enable) ? Q + 1 : Q; 16 | end 17 | 18 | assign c_enable = enable; 19 | assign c_load = (reset | ((Q == 12) & enable)); 20 | assign c_d = c_load ? 1 : 0; 21 | 22 | count4 the_counter (clk, c_enable, c_load, c_d /*, ... */ ); 23 | 24 | endmodule 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
3 |
4 |
5 |
6 |