Skip to content

Commit

Permalink
review: Improve decoder code, improve test comments
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Dudek <mdudek@antmicro.com>
  • Loading branch information
mtdudek committed Jun 16, 2023
1 parent 5958ef4 commit f4d64f0
Showing 1 changed file with 75 additions and 17 deletions.
92 changes: 75 additions & 17 deletions xls/modules/rle/rle_dec.x
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,24 @@ pub proc RLEDec<SYMBOL_WIDTH: u32, COUNT_WIDTH: u32> {

next (tok: token, state: RLEDecState<SYMBOL_WIDTH, COUNT_WIDTH>) {
let zero_input = DecInData { symbol: bits[SYMBOL_WIDTH]:0, count: bits[COUNT_WIDTH]:0, last: false };
let empty = state.count == bits[COUNT_WIDTH]:0;
let (input_tok, input) = recv_if(tok, input_r, empty, zero_input);
let (next_symbol, next_count, next_last) = if (empty) {
let t_count = input.count - bits[COUNT_WIDTH]:1;
(input.symbol, t_count, input.last)
let recv_next_symbol = (state.count == bits[COUNT_WIDTH]:0);
let (tok, input) = recv_if(tok, input_r, recv_next_symbol, zero_input);
let (next_symbol, count, next_last) = if (recv_next_symbol) {
(input.symbol, input.count, input.last)
} else {
let t_count = state.count - bits[COUNT_WIDTH]:1;
(state.symbol, t_count, state.last)
(state.symbol, state.count, state.last)
};
let send_last = next_last & (next_count == bits[COUNT_WIDTH]:0);
let data_tok = send(input_tok, output_s, DecOutData {symbol: next_symbol, last: send_last});
RLEDecState {
symbol: next_symbol,
count: next_count,
last: next_last,
let next_count = count - bits[COUNT_WIDTH]:1;
let send_last = next_last && (next_count == bits[COUNT_WIDTH]:0);
let data_tok = send(tok, output_s, DecOutData {symbol: next_symbol, last: send_last});
if (send_last) {
zero!<RLEDecState>()
} else {
RLEDecState {
symbol: next_symbol,
count: next_count,
last: next_last,
}
}
}
}
Expand Down Expand Up @@ -147,6 +150,60 @@ proc RLEDecTightTester {

next(tok: token, state: ()) {

// Single symbol transaction
let trans_send: (TestSymbol, TestCount)[1] =
[(TestSymbol:0xA, TestCount:0x1)];
let tok = for ((counter, (symbol_in, count_in)), tok): ((u32, (TestSymbol, TestCount)) , token) in enumerate(trans_send) {
let _last = counter == (array_size(trans_send) - u32:1);
let data_in = TestDecInData{symbol: symbol_in, count: count_in, last: _last};
let tok = send(tok, dec_input_s, data_in);
let _ = trace_fmt!("Sent {} transactions, symbol: 0x{:x}, count:{}, last: {}",
counter, data_in.symbol, data_in.count, data_in.last);
(tok)
}(tok);
let trans_recv: TestSymbol[1] = [
TestSymbol: 0xA,
];
let tok = for ((counter, symbol_out), tok): ((u32, TestSymbol) , token) in enumerate(trans_recv) {
let _last = counter == (array_size(trans_recv) - u32:1);
let data_out = TestDecOutData{symbol: symbol_out, last: _last};
let (tok, dec_output) = recv(tok, dec_output_r);
let _ = trace_fmt!(
"Received {} transactions, symbol: 0x{:x}, last: {}",
counter, dec_output.symbol, dec_output.last
);
let _ = assert_eq(dec_output, data_out);
(tok)
}(tok);

// Single symbol repeating transaction
let trans_send: (TestSymbol, TestCount)[2] = [
(TestSymbol:0xA, TestCount:0x3), (TestSymbol:0xA, TestCount:0x3),
];
let tok = for ((counter, (symbol_in, count_in)), tok): ((u32, (TestSymbol, TestCount)) , token) in enumerate(trans_send) {
let _last = counter == (array_size(trans_send) - u32:1);
let data_in = TestDecInData{symbol: symbol_in, count: count_in, last: _last};
let tok = send(tok, dec_input_s, data_in);
let _ = trace_fmt!("Sent {} transactions, symbol: 0x{:x}, count:{}, last: {}",
counter, data_in.symbol, data_in.count, data_in.last);
(tok)
}(tok);
let trans_recv: TestSymbol[6] = [
TestSymbol: 0xA, TestSymbol: 0xA, TestSymbol: 0xA,
TestSymbol: 0xA, TestSymbol: 0xA, TestSymbol: 0xA,
];
let tok = for ((counter, symbol_out), tok): ((u32, TestSymbol) , token) in enumerate(trans_recv) {
let _last = counter == (array_size(trans_recv) - u32:1);
let data_out = TestDecOutData{symbol: symbol_out, last: _last};
let (tok, dec_output) = recv(tok, dec_output_r);
let _ = trace_fmt!(
"Received {} transactions, symbol: 0x{:x}, last: {}",
counter, dec_output.symbol, dec_output.last
);
let _ = assert_eq(dec_output, data_out);
(tok)
}(tok);

// Simple transaction without repeats
let trans_send: (TestSymbol, TestCount)[2] =
[(TestSymbol:0xA, TestCount:0x3), (TestSymbol:0xB, TestCount:0x1)];
Expand Down Expand Up @@ -205,20 +262,21 @@ proc RLEDecTightTester {
(tok)
}(tok);

// `last` after `last` check
// Check that `next_symbol` is correctly set
let trans_send: TestDecInData[2] =[
TestDecInData {symbol: TestSymbol:0x1, count: TestCount:0x1, last:true},
TestDecInData {symbol: TestSymbol:0x1, count: TestCount:0x1, last:true},
TestDecInData {symbol: TestSymbol:0x2, count: TestCount:0x2, last:true},
];
let tok = for ((counter, data_in), tok): ((u32, TestDecInData) , token) in enumerate(trans_send) {
let tok = send(tok, dec_input_s, data_in);
let _ = trace_fmt!("Sent {} transactions, symbol: 0x{:x}, count:{}, last: {}",
counter, data_in.symbol, data_in.count, data_in.last);
(tok)
}(tok);
let trans_recv: TestDecOutData[2] = [
TestDecOutData{symbol: TestSymbol: 0x1, last: true},
let trans_recv: TestDecOutData[3] = [
TestDecOutData{symbol: TestSymbol: 0x1, last: true},
TestDecOutData{symbol: TestSymbol: 0x2, last: false},
TestDecOutData{symbol: TestSymbol: 0x2, last: true},
];
let tok = for ((counter, data_out), tok): ((u32, TestDecOutData) , token) in enumerate(trans_recv) {
let (tok, dec_output) = recv(tok, dec_output_r);
Expand Down

0 comments on commit f4d64f0

Please sign in to comment.