DEV Community

Discussion on: Share a code snippet in any programming language and describe what's going on

Collapse
 
jrothlander profile image
jrothlander • Edited

How about Verilog. This is typically one of the first ones you work through as an example. This is a 4-Bit binary counter that counts from 0 to 9 and displays the count on a 7-segment display. The display is just 7 LEDs, so it turns all of the on by default and you use a binary number to tell it which of the LEDs to turn off.

While Verilog is language, it a hardware descriptive language, meaning that it tells an FPGA (field programmable gate array) how to setup its hardware. So you are creating a chip at runtime and can rework it and reprogram it on the fly. You have one in your phone and they are great for AI. So a little different from a typical program. It is a way to implement software as hardware.

module top(                     
    input wire clk,
    output wire LED0,           // bit 1
    output wire LED1,           // bit 2
    output wire LED2,           // bit 4
    output wire LED3,           // bit 8
    output reg [6:0] LED_out,   // 7-segment LED display[0]
    output reg [6:0] LED_out1       // 7-segment LED display[1]
);    

    reg [31:0] cnt;                 // 32-bit counter   
    reg [3:0] LED_BCD;  
    reg [31:0] startbit = 25;       // starting counter index

    // assign LEDs to count bits, higher the index, the slower the count will run. 
    assign LED0 = cnt[startbit];
    assign LED1 = cnt[startbit + 1];
    assign LED2 = cnt[startbit + 2];
    assign LED3 = cnt[startbit + 3];        

    initial begin 
        cnt <= 32'h0; // start at zero
    end

    always @(posedge clk) begin
        cnt <= cnt + 1;             
        LED_BCD[0] <= cnt[startbit];
        LED_BCD[1] <= cnt[startbit + 1];
        LED_BCD[2] <= cnt[startbit + 2];
        LED_BCD[3] <= cnt[startbit + 3];

        if (LED_BCD  == 4'b1010)        // if LED_BCD equals 10 
            cnt <= 0;                       // reset
    end

    // 7-Segment LED display patterns
    always @(*)
        begin
            LED_out1 = 7'b11111111;   // "0"                                                                                
            case(LED_BCD)      // gfedcba                                                       
                4'h0: //LED_out  = 7'b1000000;   // "0"     
                    begin
                        LED_out = 7'b1000000;   // "0"                                                          
                        LED_out1 = 7'b1111001;   // "1"                                                                                 
                    end             
                4'h1: LED_out  = 7'b1111001; // "1"                         
                4'h2: LED_out = 7'b0100100;     // "2" 
                4'h3: LED_out = 7'b0110000;     // "3" 
                4'h4: LED_out = 7'b0011001;     // "4" 
                4'h5: LED_out = 7'b0010010;     // "5" 
                4'h6: LED_out = 7'b0000010;     // "6" 
                4'h7: LED_out = 7'b1111000;     // "7" 
                4'h8: LED_out = 7'b0000000;     // "8"  
                4'h9: LED_out = 7'b0010000;   // "9"                                                                            
            endcase
        end         
endmodule
Enter fullscreen mode Exit fullscreen mode

And you can't ignore Assembly. Here's an example I was working on from Ben Eater. It prints "Hello World". This is why we don't write in Assembly anymore.

PORTB = $6000
PORTA = $6001
DDRB = $6002
DDRA = $6003

E  = %10000000
RW = %01000000 m                                                   
RS = %00100000

  .org $8000

reset:
  lda #%11111111 ; Set all pins on port B to output
  sta DDRB

  lda #%11100000 ; Set top 3 pins on port A to output
  sta DDRA

  lda #%00111000 ; Set 8-bit mode; 2-line display; 5x8 font
  sta PORTB
  lda #0         ; Clear RS/RW/E bits
  sta PORTA
  lda #E         ; Set E bit to send instruction
  sta PORTA
  lda #0         ; Clear RS/RW/E bits
  sta PORTA

  lda #%00001110 ; Display on; cursor on; blink off
  sta PORTB
  lda #0         ; Clear RS/RW/E bits
  sta PORTA
  lda #E         ; Set E bit to send instruction
  sta PORTA
  lda #0         ; Clear RS/RW/E bits
  sta PORTA

  lda #%00000110 ; Increment and shift cursor; don't shift display
  sta PORTB
  lda #0         ; Clear RS/RW/E bits
  sta PORTA
  lda #E         ; Set E bit to send instruction
  sta PORTA
  lda #0         ; Clear RS/RW/E bits
  sta PORTA

  lda #"H"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"e"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"l"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"l"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"o"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #","
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #" "
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"w"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"o"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"r"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"l"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"d"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

  lda #"!"
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA

loop:
  jmp loop

  .org $fffc
  .word reset
  .word $0000
Enter fullscreen mode Exit fullscreen mode