Attention
This documentation is a work in progress. Expect to see errors and unfinished things.
sat_add Source File
1`timescale 1ns / 1ns
2
3module sat_add #(
4 parameter isize=16,
5 parameter osize=15
6) (
7 input clk,
8 input signed [isize-1:0] a,
9 input signed [isize-1:0] b,
10 output signed [osize-1:0] sum
11);
12 // why do I need to explicitly sign-extend?
13 wire signed [isize:0] s = {a[isize-1],a} + {b[isize-1],b};
14 wire ok = ~(|s[isize:osize-1]) | (&s[isize:osize-1]);
15 reg signed [osize-1:0] sr=0;
16 always @(posedge clk)
17 sr <= ok ? s[osize-1:0] : {s[isize],{osize-1{~s[isize]}}};
18 assign sum = sr;
19endmodule