| 1 | <p>Note that this package supports syntax highlighting for both Verilog and System Verilog.</p> |
| 2 | |
| 3 | <h2>Comments</h2> |
| 4 | <pre><code>/* Multiline comments in Verilog |
| 5 | look like C comments and // is OK in here. */ |
| 6 | // Single-line comment in Verilog.</code></pre> |
| 7 | |
| 8 | <h2>Literals</h2> |
| 9 | <pre><code>// example code from: http://iroi.seu.edu.cn/books/asics/Book2/CH11/CH11.02.htm |
| 10 | module declarations; |
| 11 | parameter H12_UNSIZED = 'h 12; |
| 12 | parameter H12_SIZED = 6'h 12; |
| 13 | parameter D42 = 8'B0010_1010; |
| 14 | parameter D123 = 123; |
| 15 | parameter D63 = 8'o 77; |
| 16 | parameter A = 'h x, B = 'o x, C = 8'b x, D = 'h z, E = 16'h ????; |
| 17 | reg [3:0] B0011,Bxxx1,Bzzz1; |
| 18 | real R1,R2,R3; |
| 19 | integer I1,I3,I_3; |
| 20 | parameter BXZ = 8'b1x0x1z0z; |
| 21 | |
| 22 | initial begin |
| 23 | B0011 = 4'b11; Bxxx1 = 4'bx1; Bzzz1 = 4'bz1; |
| 24 | R1 = 0.1e1; R2 = 2.0; R3 = 30E-01; |
| 25 | I1 = 1.1; I3 = 2.5; I_3 = -2.5; |
| 26 | end |
| 27 | |
| 28 | initial begin #1; |
| 29 | $display("H12_UNSIZED, H12_SIZED (hex) = %h, %h",H12_UNSIZED, H12_SIZED); |
| 30 | $display("D42 (bin) = %b",D42," (dec) = %d",D42); |
| 31 | $display("D123 (hex) = %h",D123," (dec) = %d",D123); |
| 32 | $display("D63 (oct) = %o",D63); |
| 33 | $display("A (hex) = %h",A," B (hex) = %h",B); |
| 34 | $display("C (hex) = %h",C," D (hex) = %h",D," E (hex) = %h",E); |
| 35 | $display("BXZ (bin) = %b",BXZ," (hex) = %h",BXZ); |
| 36 | $display("B0011, Bxxx1, Bzzz1 (bin) = %b, %b, %b",B0011,Bxxx1,Bzzz1); |
| 37 | $display("R1, R2, R3 (e, f, g) = %e, %f, %g", R1, R2, R3); |
| 38 | $display("I1, I3, I_3 (d) = %d, %d, %d", I1, I3, I_3); |
| 39 | end |
| 40 | endmodule</code></pre> |
| 41 | |
| 42 | <h2>Full example</h2> |
| 43 | <pre><code>`include "internal_defines.vh" |
| 44 | |
| 45 | //***************************************************************************** |
| 46 | // memory_decoder: a custom module used to handle memory transactions |
| 47 | //***************************************************************************** |
| 48 | // |
| 49 | // out_mem (output) - The output to memory |
| 50 | // out_reg (output) - The output to the register file |
| 51 | // mem_we (output) - Which byte in the word to write too |
| 52 | // mem_in (input) - The input from memory |
| 53 | // addr_in (input) - The lowest 2 bits of byte offset to store in memory |
| 54 | // data_in (input) - The input from the register file to be stored |
| 55 | // l_bit (input) - The load bit signal (control) |
| 56 | // b_bit (input) - The byte bit signal (control) |
| 57 | // |
| 58 | module memory_decoder(out_mem, out_reg, mem_in, data_in, l_bit, b_bit, addr_in, |
| 59 | mem_we); |
| 60 | |
| 61 | output reg [31:0] out_mem, out_reg; |
| 62 | output reg [3:0] mem_we; |
| 63 | input [31:0] mem_in, data_in; |
| 64 | input [1:0] addr_in; |
| 65 | input l_bit, b_bit; |
| 66 | |
| 67 | always_comb begin |
| 68 | mem_we = 4'b0000; // dont write memory by default |
| 69 | if (l_bit == 1) begin // ldr and ldrb |
| 70 | out_mem = mem_in; // dont change memory! |
| 71 | if (b_bit == 1) begin |
| 72 | /* figure out which byte to load from memory */ |
| 73 | case (addr_in) |
| 74 | 2'b00: out_reg = {24'b00, mem_in[7:0]}; |
| 75 | 2'b01: out_reg = {24'b00, mem_in[15:8]}; |
| 76 | 2'b10: out_reg = {24'b00, mem_in[23:16]}; |
| 77 | 2'b11: out_reg = {24'b00, mem_in[31:24]}; |
| 78 | endcase |
| 79 | end |
| 80 | else begin |
| 81 | out_reg = mem_in; |
| 82 | end |
| 83 | end |
| 84 | else begin // str and strb |
| 85 | out_reg = `UNKNOWN; // We are not reading from mem |
| 86 | if (b_bit == 1) begin |
| 87 | /* figure out which byte to write to in memory */ |
| 88 | out_mem = {4{data_in[7:0]}}; |
| 89 | case (addr_in) |
| 90 | 2'b00: mem_we = 4'b1000; |
| 91 | 2'b01: mem_we = 4'b0100; |
| 92 | 2'b10: mem_we = 4'b0010; |
| 93 | 2'b11: mem_we = 4'b0001; |
| 94 | endcase |
| 95 | end |
| 96 | else begin |
| 97 | mem_we = 4'b1111; // write to all channels |
| 98 | out_mem = data_in; |
| 99 | end |
| 100 | end |
| 101 | end |
| 102 | |
| 103 | endmodule</code></pre> |