|
| 1 | +// Copyright 2024 The XLS Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// This file contains implementation of a Buffer structure that acts as |
| 16 | +// a simple FIFO. Additionally, the file provides various functions that |
| 17 | +// can simplify access to the stored. |
| 18 | +// |
| 19 | +// The utility functions containing the `_checked` suffix serve two purposes: |
| 20 | +// they perform the actual operation and return information on whether |
| 21 | +// the operation was successful. If you are sure that the precondition is |
| 22 | +// always true, you can use the function with the same name but without |
| 23 | +// the `_checked` suffix. |
| 24 | + |
| 25 | +import std; |
| 26 | +import xls.modules.zstd.buffer as buff; |
| 27 | + |
| 28 | +type Buffer = buff::Buffer; |
| 29 | + |
| 30 | +// WindowBuffer is a simple Proc that uses the Buffer structure to aggregate data |
| 31 | +// in transactions of <INPUT_WIDTH> length and output it in transactions of |
| 32 | +// <OUTPUT_WIDTH> length. <BUFFER_SIZE> defines the maximal size of the buffer. |
| 33 | + |
| 34 | +proc WindowBuffer<BUFFER_SIZE: u32, INPUT_WIDTH: u32, OUTPUT_WIDTH: u32> { |
| 35 | + input_r: chan<uN[INPUT_WIDTH]> in; |
| 36 | + output_s: chan<uN[OUTPUT_WIDTH]> out; |
| 37 | + |
| 38 | + config( |
| 39 | + input_r: chan<uN[INPUT_WIDTH]> in, |
| 40 | + output_s: chan<uN[OUTPUT_WIDTH]> out |
| 41 | + ) { (input_r, output_s) } |
| 42 | + |
| 43 | + init { buff::buffer_new<BUFFER_SIZE>() } |
| 44 | + |
| 45 | + next(tok: token, buffer: Buffer<BUFFER_SIZE>) { |
| 46 | + let (tok, recv_data) = recv(tok, input_r); |
| 47 | + let buffer = buff::buffer_append<BUFFER_SIZE>(buffer, recv_data); |
| 48 | + |
| 49 | + if buffer.length >= OUTPUT_WIDTH { |
| 50 | + let (buffer, data_to_send) = buff::buffer_fixed_pop<BUFFER_SIZE, OUTPUT_WIDTH>(buffer); |
| 51 | + let tok = send(tok, output_s, data_to_send); |
| 52 | + buffer |
| 53 | + } else { |
| 54 | + buffer |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[test_proc] |
| 60 | +proc WindowBufferTest { |
| 61 | + terminator: chan<bool> out; |
| 62 | + data32_s: chan<u32> out; |
| 63 | + data48_r: chan<u48> in; |
| 64 | + |
| 65 | + config(terminator: chan<bool> out) { |
| 66 | + let (data32_s, data32_r) = chan<u32>; |
| 67 | + let (data48_s, data48_r) = chan<u48>; |
| 68 | + spawn WindowBuffer<u32:64, u32:32, u32:48>(data32_r, data48_s); |
| 69 | + (terminator, data32_s, data48_r) |
| 70 | + } |
| 71 | + |
| 72 | + init {} |
| 73 | + |
| 74 | + next(tok: token, state: ()) { |
| 75 | + let tok = send(tok, data32_s, u32:0xDEADBEEF); |
| 76 | + let tok = send(tok, data32_s, u32:0xBEEFCAFE); |
| 77 | + let tok = send(tok, data32_s, u32:0xCAFEDEAD); |
| 78 | + |
| 79 | + let (tok, received_data) = recv(tok, data48_r); |
| 80 | + assert_eq(received_data, u48:0xCAFE_DEAD_BEEF); |
| 81 | + let (tok, received_data) = recv(tok, data48_r); |
| 82 | + assert_eq(received_data, u48:0xCAFE_DEAD_BEEF); |
| 83 | + |
| 84 | + send(tok, terminator, true); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Sample for codegen |
| 89 | +proc WindowBuffer64 { |
| 90 | + input_r: chan<u32> in; |
| 91 | + output_s: chan<u48> out; |
| 92 | + |
| 93 | + config( |
| 94 | + input_r: chan<u32> in, |
| 95 | + output_s: chan<u48> out |
| 96 | + ) { |
| 97 | + spawn WindowBuffer<u32:64, u32:32, u32:48>(input_r, output_s); |
| 98 | + (input_r, output_s) |
| 99 | + } |
| 100 | + |
| 101 | + init {} |
| 102 | + |
| 103 | + next(tok: token, state: ()) {} |
| 104 | +} |
0 commit comments