Skip to content

About

my verilog notebook and implementation of various logic gates while learning hardware-software codesign :D

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Verilog

My digital design notes written out as working Verilog. Every file is one self contained module, grouped by what it does, starting at single gates and building up to arithmetic units, state machines and memories.

Everything here is synthesisable Verilog-2001 ($clog2 aside) and compiles clean under Icarus Verilog.

Running something

iverilog -g2005 -o sim.out my_tb.v gates/and_gate.v
vvp sim.out

A few modules pull in another file from this repo:

module also needs
ripple_carry_adder adders/full_adder.v
carry_select_adder adders/ripple_carry_adder.v, adders/full_adder.v
seven_segment_mux converters/bcd_to_7segment.v

Widths are parameters almost everywhere, so #(.WIDTH(16)) at instantiation is usually all it takes to resize a block.

Gates

module file notes
and_gate and_gate.v two input AND
or_gate or_gate.v two input OR
not_gate not_gate.v inverter
nand_gate nand_gate.v two input NAND
nor_gate nor_gate.v two input NOR
xor_gate xor_gate.v two input XOR
xnor_gate xnor_gate.v two input XNOR
buffer buffer.v straight through
tristate_buffer tristate_buffer.v drives z when oe is low
universal_nand universal_nand.v NOT, AND, OR and XOR built from NAND only

Adders and subtractors

module file notes
half_adder half_adder.v sum and carry
full_adder full_adder.v with carry in
half_subtractor half_subtractor.v difference and borrow
full_subtractor full_subtractor.v with borrow in
ripple_carry_adder ripple_carry_adder.v N bit chain of full adders
carry_lookahead_adder carry_lookahead_adder.v 4 bit, generate and propagate terms
carry_select_adder carry_select_adder.v both carries computed per block, then picked
carry_save_adder carry_save_adder.v three operands into a sum and carry pair
adder_subtractor adder_subtractor.v two's complement, carry and overflow flags
bcd_adder bcd_adder.v one digit, +6 correction past 9
incrementer incrementer.v plus one with carry out

Multipliers

module file notes
array_multiplier array_multiplier.v 4x4 unsigned, purely combinational
shift_add_multiplier shift_add_multiplier.v sequential unsigned, one partial product per clock
booth_multiplier booth_multiplier.v radix 2 booth, signed operands

Dividers

module file notes
restoring_divider restoring_divider.v one quotient bit per clock, flags divide by zero
non_restoring_divider non_restoring_divider.v skips the restore, corrects once at the end

ALU

module file notes
alu alu.v 16 operations, zero carry and overflow flags

Opcodes: 0 add, 1 sub, 2 and, 3 or, 4 xor, 5 not, 6 sll, 7 srl, 8 sra, 9 rol, A ror, B slt, C inc, D dec, E pass a, F pass b.

Comparators

module file notes
comparator1bit 1bit_comparator.v gate level, one bit
comparator4bit 4bit_magnitude_comparator.v greater, less, equal
comparator_nbit nbit_comparator.v same thing, parameterised
signed_comparator signed_comparator.v two's complement compare

Counters

module file notes
binary_counter binary_counter.v plain up counter with enable
up_down_counter updown_counter.v direction picked by up
ripple_counter ripple_counter.v asynchronous, each stage clocks the next
mod_n_counter mod_n_counter.v wraps at N, tick on the last state
bcd_counter bcd_counter.v mod 10 with carry out
decade_updown_counter decade_updown_counter.v 0 to 9 either way
ring_counter ring_counter.v one hot bit walking round
johnson_counter johnson_counter.v twisted ring, 2N states
gray_counter gray_counter.v one bit changes per step

Flip flops

module file notes
d_flipflop d_flip_flop.v no reset
d_flipflop_sync_reset d_flipflop_sync_reset.v reset sampled on the clock
d_flipflop_async_reset d_flipflop_async_reset.v async reset plus enable
sr_flipflop sr_flipflop.v set reset, 11 is invalid
jk_flipflop jk_flipflop.v 11 toggles
t_flipflop t_flipflop.v toggle on t
master_slave_jk master_slave_jk.v two stages on opposite clock phases

Latches

module file notes
sr_latch SRlatch.v cross coupled NOR
sr_latch_nand sr_latch_nand.v cross coupled NAND, active low inputs
gated_sr_latch gated_sr_latch.v SR behind an enable
d_latch d_latch.v transparent while en is high

Registers and shift registers

module file notes
nbit_reg nbit_register.v load enable, async reset
siso siso.v serial in serial out
sipo sipo.v serial in parallel out
piso piso.v parallel in serial out, msb first
pipo pipo.v parallel in parallel out
universal_shift_register universal_shift_register.v hold, shift either way, or load
rotate_register shift_register_rotate.v rotates instead of shifting in zeros
lfsr lfsr.v fibonacci, default taps give all 255 states

Multiplexers and demultiplexers

module file notes
mux2to1 mux.v one select line
mux4to1 mux4to1.v two select lines
mux8to1 mux8to1.v three select lines
mux_nbit mux_nbit.v picks one of several buses
demux1to2 demux.v one input, two outputs
demux1to4 demux1to4.v one input, four outputs
demux1to8 demux1to8.v one input, eight outputs

Decoders and encoders

module file notes
decoder2to4 decoder.v with enable
decoder3to8 decoder3to8.v with enable
decoder_nbit decoder_nbit.v any width, one hot out
encoder4to2 encoder.v plain encoder with a valid flag
priority_encoder8to3 priority_encoder.v highest set bit wins

Parity

module file notes
even_parity_generator even_parity.v xor reduction of the data
odd_parity_generator odd_parity.v inverted xor reduction
parity_checker parity_checker.v flags a mismatch, even or odd

Code converters

module file notes
binary_to_gray binary_to_gray.v xor with the shifted value
gray_to_binary gray_to_binary.v running xor back down
binary_to_bcd binary_to_bcd.v double dabble, shift and add 3
bcd_to_binary bcd_to_binary.v weighted sum of three digits
bcd_to_7segment bcd_to_7segment.v common anode, segments active low
binary_to_excess3 binary_to_excess3.v plus three
twos_complement twos_complement.v ones and twos complement together

State machines

module file notes
sequence_detector_moore sequence_detector_moore.v 1011, overlapping, output from the state
sequence_detector_mealy sequence_detector_mealy.v same pattern, output on the last input bit
traffic_light traffic_light.v two roads, green yellow red on a tick
vending_machine vending_machine.v 5 and 10 coins, dispenses at 20 and returns change

Memories

module file notes
rom rom.v combinational read, contents set in initial
single_port_ram single_port_ram.v one address for read and write
dual_port_ram dual_port_ram.v independent read and write ports
sync_fifo sync_fifo.v single clock, depth a power of two
stack stack.v lifo with full and empty flags
register_file register_file.v two reads one write, register zero reads zero

Building blocks

module file notes
clock_divider clock_divider.v divided clock output
clock_enable clock_enable.v one cycle tick, safer than a divided clock
edge_detector edge_detector.v rise, fall and any
synchronizer synchronizer.v two flop crossing into this clock domain
debouncer debouncer.v input has to hold steady before it counts
pulse_stretcher pulse_stretcher.v holds a one cycle pulse
barrel_shifter barrel_shifter.v shift or rotate, either way, optional sign extend
pwm pwm.v duty out of 2**WIDTH
population_count population_count.v counts the ones
seven_segment_mux seven_segment_mux.v four digits onto one segment bus

About

my verilog notebook and implementation of various logic gates while learning hardware-software codesign :D

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages