-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
modules/zstd: add buffer module #1167
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
This commit adds a simple test that shows, how one can use the Buffer struct inside a Proc. Internal-tag: [#50221] Signed-off-by: Robert Winkler <rwinkler@antmicro.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2024 The XLS Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// This file contains the implementation of a Proc which can be used to | ||
// receive data through transactions of one width and output that data | ||
// in transactions of other width. | ||
|
||
import std; | ||
import xls.modules.zstd.buffer as buff; | ||
|
||
type Buffer = buff::Buffer; | ||
|
||
// WindowBuffer is a simple Proc that uses the Buffer structure to aggregate data | ||
// in transactions of <INPUT_WIDTH> length and output it in transactions of | ||
// <OUTPUT_WIDTH> length. <BUFFER_SIZE> defines the maximal size of the buffer. | ||
|
||
proc WindowBuffer<BUFFER_SIZE: u32, INPUT_WIDTH: u32, OUTPUT_WIDTH: u32> { | ||
input_r: chan<uN[INPUT_WIDTH]> in; | ||
output_s: chan<uN[OUTPUT_WIDTH]> out; | ||
|
||
config( | ||
input_r: chan<uN[INPUT_WIDTH]> in, | ||
output_s: chan<uN[OUTPUT_WIDTH]> out | ||
) { (input_r, output_s) } | ||
|
||
init { buff::buffer_new<BUFFER_SIZE>() } | ||
|
||
next(tok: token, buffer: Buffer<BUFFER_SIZE>) { | ||
const_assert!(BUFFER_SIZE >= INPUT_WIDTH); | ||
const_assert!(BUFFER_SIZE >= OUTPUT_WIDTH); | ||
let (tok, recv_data, valid) = recv_non_blocking(tok, input_r, uN[INPUT_WIDTH]:0); | ||
let buffer = if (valid) { | ||
buff::buffer_append(buffer, recv_data) | ||
} else { | ||
buffer | ||
}; | ||
|
||
if buffer.length >= OUTPUT_WIDTH { | ||
let (buffer, data_to_send) = buff::buffer_fixed_pop<OUTPUT_WIDTH>(buffer); | ||
let tok = send(tok, output_s, data_to_send); | ||
buffer | ||
} else { | ||
buffer | ||
} | ||
} | ||
} | ||
|
||
#[test_proc] | ||
proc WindowBufferTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add a test with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
terminator: chan<bool> out; | ||
data32_s: chan<u32> out; | ||
data48_r: chan<u48> in; | ||
|
||
config(terminator: chan<bool> out) { | ||
let (data32_s, data32_r) = chan<u32>; | ||
let (data48_s, data48_r) = chan<u48>; | ||
spawn WindowBuffer<u32:64, u32:32, u32:48>(data32_r, data48_s); | ||
(terminator, data32_s, data48_r) | ||
} | ||
|
||
init {} | ||
|
||
next(tok: token, state: ()) { | ||
let tok = send(tok, data32_s, u32:0xDEADBEEF); | ||
let tok = send(tok, data32_s, u32:0xBEEFCAFE); | ||
let tok = send(tok, data32_s, u32:0xCAFEDEAD); | ||
|
||
let (tok, received_data) = recv(tok, data48_r); | ||
assert_eq(received_data, u48:0xCAFE_DEAD_BEEF); | ||
let (tok, received_data) = recv(tok, data48_r); | ||
assert_eq(received_data, u48:0xCAFE_DEAD_BEEF); | ||
|
||
send(tok, terminator, true); | ||
} | ||
} | ||
|
||
#[test_proc] | ||
proc WindowBufferReverseTest { | ||
terminator: chan<bool> out; | ||
data48_s: chan<u48> out; | ||
data32_r: chan<u32> in; | ||
|
||
config(terminator: chan<bool> out) { | ||
let (data48_s, data48_r) = chan<u48>; | ||
let (data32_s, data32_r) = chan<u32>; | ||
spawn WindowBuffer<u32:64, u32:48, u32:32>(data48_r, data32_s); | ||
(terminator, data48_s, data32_r) | ||
} | ||
|
||
init {} | ||
|
||
next(tok: token, state: ()) { | ||
let tok = send(tok, data48_s, u48:0xCAFEDEADBEEF); | ||
let tok = send(tok, data48_s, u48:0xCAFEDEADBEEF); | ||
|
||
let (tok, received_data) = recv(tok, data32_r); | ||
assert_eq(received_data, u32:0xDEADBEEF); | ||
let (tok, received_data) = recv(tok, data32_r); | ||
assert_eq(received_data, u32:0xBEEFCAFE); | ||
let (tok, received_data) = recv(tok, data32_r); | ||
assert_eq(received_data, u32:0xCAFEDEAD); | ||
|
||
send(tok, terminator, true); | ||
} | ||
} | ||
|
||
// Sample for codegen | ||
proc WindowBuffer64 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for adding this! |
||
input_r: chan<u32> in; | ||
output_s: chan<u48> out; | ||
|
||
config( | ||
input_r: chan<u32> in, | ||
output_s: chan<u48> out | ||
) { | ||
spawn WindowBuffer<u32:64, u32:32, u32:48>(input_r, output_s); | ||
(input_r, output_s) | ||
} | ||
|
||
init {} | ||
|
||
next(tok: token, state: ()) {} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a relation between
BUFFER_SIZE
,INPUT_WIDTH
andOUTPUT_WIDTH
that we canconst_assert
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can for sure assert
BUFFER_SIZE >= INPUT WIDTH
andBUFFER_SIZE >= OUTPUT_WIDTH
.