diff options
| author | Julian Blake Kongslie | 2022-03-26 09:50:26 -0700 |
|---|---|---|
| committer | Julian Blake Kongslie | 2022-03-26 09:50:26 -0700 |
| commit | 4541224c007a1818ffdcd69a7dc4a8e5392bb44d (patch) | |
| tree | 2958e8b02ae60c3acd5559aaa4e821678b5dc326 /hdl | |
| parent | Run ~*EIGHT GODDAMN PDP-8s IN PARALLEL*~ (diff) | |
| download | multipdp8-4541224c007a1818ffdcd69a7dc4a8e5392bb44d.tar.xz | |
Add basic clock-domain-crossing FIFO.
Diffstat (limited to 'hdl')
| -rw-r--r-- | hdl/fifo.sv | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/hdl/fifo.sv b/hdl/fifo.sv new file mode 100644 index 0000000..4158067 --- /dev/null +++ b/hdl/fifo.sv | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | module fifo | ||
| 2 | #( type data_t = bit | ||
| 3 | |||
| 4 | , DEPTH_BITS = 10 | ||
| 5 | ) | ||
| 6 | ( input bit clock_in | ||
| 7 | , input bit clock_out | ||
| 8 | , input bit reset | ||
| 9 | |||
| 10 | , output bit in_ready | ||
| 11 | , input bit in_valid | ||
| 12 | , input data_t in_data | ||
| 13 | |||
| 14 | , input bit out_ready | ||
| 15 | , output bit out_valid | ||
| 16 | , output data_t out_data | ||
| 17 | ); | ||
| 18 | |||
| 19 | localparam DEPTH = 1<<DEPTH_BITS; | ||
| 20 | |||
| 21 | typedef bit [DEPTH_BITS-1:0] addr_t; | ||
| 22 | |||
| 23 | data_t data [DEPTH-1:0]; | ||
| 24 | |||
| 25 | addr_t oldest, youngest; | ||
| 26 | bit oldest_wrap, youngest_wrap; | ||
| 27 | |||
| 28 | addr_t oldest_grey, youngest_grey; | ||
| 29 | |||
| 30 | always @(posedge clock_in, posedge reset) begin | ||
| 31 | if (reset) begin | ||
| 32 | in_ready = 0; | ||
| 33 | youngest = 0; | ||
| 34 | youngest_wrap = 0; | ||
| 35 | youngest_grey = 0; | ||
| 36 | end else begin | ||
| 37 | if (in_ready && in_valid) begin | ||
| 38 | data[youngest] = in_data; | ||
| 39 | if (++youngest == 0) | ||
| 40 | ++youngest_wrap; | ||
| 41 | end | ||
| 42 | |||
| 43 | youngest_grey = youngest ^ (youngest >> 1); | ||
| 44 | |||
| 45 | in_ready = oldest_grey != youngest_grey || youngest_wrap == oldest_wrap; | ||
| 46 | end | ||
| 47 | end | ||
| 48 | |||
| 49 | always @(posedge clock_out, posedge reset) begin | ||
| 50 | if (reset) begin | ||
| 51 | out_valid = 0; | ||
| 52 | oldest = 0; | ||
| 53 | oldest_wrap = 0; | ||
| 54 | oldest_grey = 0; | ||
| 55 | end else begin | ||
| 56 | if (out_ready && out_valid) begin | ||
| 57 | if (++oldest == 0) | ||
| 58 | ++oldest_wrap; | ||
| 59 | end | ||
| 60 | |||
| 61 | oldest_grey = oldest ^ (oldest >> 1); | ||
| 62 | |||
| 63 | out_valid = oldest_grey != youngest_grey || youngest_wrap != oldest_wrap; | ||
| 64 | out_data = data[oldest]; | ||
| 65 | end | ||
| 66 | end | ||
| 67 | |||
| 68 | endmodule | ||
