tvm_marcos.v 4.13 KB
Newer Older
1 2 3 4 5
// Nonstop version of loop
// Always keeps looping when increase == true
// At end is a signal to indicate the next cycle is end
// Use that to signal parent loop to advance.
`define NONSTOP_LOOP(iter, width, init, ready, finish, min, extent)\
6
    reg [width-1:0] iter;\
7
    wire finish;\
8
    always@(posedge clk) begin\
9
        if (rst || init) begin\
10
            iter <= (min);\
11 12 13
        end else if(ready) begin\
            if (iter != ((extent)-1)) begin\
                iter <= iter + 1;\
14
            end else begin\
15
              iter <= (min);\
16 17 18 19
            end\
        end else begin\
            iter <= iter;\
        end\
20 21
    end\
    assign finish = (ready && (iter == (extent) - 1));
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

// Wrap a nonstop loop to normal loop that loop only once.
// Use done signal to control the non-stop body to stop.
// The init and done behaves like normal loop
`define WRAP_LOOP_ONCE(init, valid, ready, body_finish, body_ready)\
    reg valid;\
    wire body_ready;\
    always@(posedge clk) begin\
        if (rst || init) begin\
            valid <= 1;\
        end else if(body_finish) begin\
            valid <= 0;\
        end else begin\
            valid <= valid;\
        end\
    end\
    assign body_ready = (valid && ready);

// Assign dst as src delayed by specific cycles.
`define DELAY(dst, src, width, delay, not_stall)\
    reg [(width)*(delay)-1:0] src``_dly_chain;\
44 45
    always@(posedge clk) begin\
        if(rst) begin\
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
            src``_dly_chain <= 0;\
        end else if (not_stall) begin\
            src``_dly_chain[(width)-1:0] <= src;\
            if((delay) != 1) begin\
                src``_dly_chain[(delay)*(width)-1:(width)] <= src``_dly_chain[((delay)-1)*(width)-1:0];\
            end\
        end else begin\
            src``_dly_chain <= src``_dly_chain;\
        end\
    end\
    assign dst = src``_dly_chain[(delay)*(width)-1:((delay)-1)*(width)];

// TVM generate clock signal
`define TVM_DEFINE_TEST_SIGNAL(clk, rst)\
   parameter PER = 10;\
   reg clk;\
   reg rst;\
   always begin\
      #(PER/2) clk =~ clk;\
   end

// Control logic on buffer/RAM read valid.
// This delays the valid signal by one cycle and retain it when write_ready == 0
`define BUFFER_READ_VALID_DELAY(dst, data_valid, write_ready)\
    reg dst;\
    always@(posedge clk) begin\
        if(rst) begin\
            dst <= 0;\
        end else if (write_ready) begin\
            dst <= (data_valid);\
        end else begin\
            dst <= dst;\
        end\
    end\

// A cache register that add one cycle lag to the ready signal
// This allows the signal to flow more smoothly
`define CACHE_REG(width, in_data, in_valid, in_ready, out_data, out_valid, out_ready)\
    reg [width-1:0] out_data``_state_;\
    reg [width-1:0] out_data``_overflow_;\
    reg out_valid``_state_;\
    reg out_valid``_overflow_;\
    always@(posedge clk) begin\
        if(rst) begin\
            out_valid``_overflow_ <= 0;\
            out_valid``_state_ <= 0;\
        end else if (out_valid``_overflow_) begin\
            if (out_ready) begin\
               out_valid``_state_ <= 1;\
               out_data``_state_ <= out_data``_overflow_;\
               out_valid``_overflow_ <= 0;\
               out_data``_overflow_ <= 0;\
98
            end else begin\
99 100 101 102
               out_valid``_state_ <= 1;\
               out_data``_state_ <= out_data``_state_;\
               out_valid``_overflow_ <= out_valid``_overflow_;\
               out_data``_overflow_ <= out_data``_overflow_;\
103 104
            end\
        end else begin\
105 106 107 108 109 110 111 112 113 114 115
            if (!out_ready && out_valid``_state_) begin\
               out_valid``_state_ <= 1;\
               out_data``_state_ <= out_data``_state_;\
               out_valid``_overflow_ <= in_valid;\
               out_data``_overflow_ <= in_data;\
            end else begin\
               out_valid``_state_ <= in_valid;\
               out_data``_state_ <= in_data;\
               out_valid``_overflow_ <= out_valid``_overflow_;\
               out_data``_overflow_ <= out_data``_overflow_;\
            end\
116
        end\
117 118 119 120
    end\ // always@ (posedge clk)
    assign in_ready = !out_valid``_overflow_;\
    assign out_data = out_data``_state_;\
    assign out_valid = out_valid``_state_;