# Simple Adder Example
This example assigns the outputs A and B as the sum and difference of the inputs A and B.
OutputA is Input A + Input B;
OutputB is Input A - Input B;
-- A very simple example, simply add two inputs and route to an output.
-- This is purely combinatorial
architecture Behavioural of CustomWrapper is
begin
OutputA <= InputA + InputB;
OutputB <= InputA - InputB;
end architecture;
1
2
3
4
5
6
7
2
3
4
5
6
7
# Voltage limiter example
This example uses the clip function from the Moku Library to limit the output signal to a set range. The upper limit of Output A is set by Control0, the lower limit of Output A is set by Control1. The upper limit of Output B is set by Control2, the lower limit of Output B is set by Control3.
library IEEE;
use IEEE.Numeric_Std.all;
library Moku;
use Moku.Support.clip_val;
use Moku.Support.sum_no_overflow;
architecture Behavioural of CustomWrapper is
signal ch1_lower : signed(15 downto 0);
signal ch1_upper : signed(15 downto 0);
signal ch2_lower : signed(15 downto 0);
signal ch2_upper : signed(15 downto 0);
begin
ch1_lower <= signed(Control0(15 downto 0));
ch1_upper <= signed(Control1(15 downto 0));
ch2_lower <= signed(Control2(15 downto 0));
ch2_upper <= signed(Control3(15 downto 0));
-- Use library function to "clip" the value to within the upper and lower bounds
OutputA <= clip_val(InputA, to_integer(ch1_lower), to_integer(ch1_upper));
OutputB <= clip_val(InputB, to_integer(ch2_lower), to_integer(ch2_upper));
end architecture;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# DSP example
This example instantiates a DSP block using the ScaleOffset wrapper. The Moku.Support.ScaleOffset
entity conveniently packages a DSP block with all the settings configured to compute the common Z = X * Scale + Offset
operation, with the output properly clipped to prevent under/overflow.
# Getting Started
# Signals and Settings
Port | Use |
---|---|
Control0 | Scale A |
Control1 | Offset A |
Control2 | Scale B |
Control3 | Offset B |
Output A | Scaled and Offset Input A |
Output B | Scaled and Offset Input B |
library IEEE;
use IEEE.Numeric_Std.all;
library Moku;
use Moku.Support.ScaleOffset;
-- Instantiate a DSP block using the ScaleOffset wrapper
architecture Behavioural of CustomWrapper is
begin
-- Z = X * Scale + Offset
-- Offset is units of bits, scale by default runs from -1 to 1 across whatever signal width is given
-- Clips Z to min/max (prevents over/underflow)
-- Includes rounding
-- One Clock Cycle Delay
DSP: ScaleOffset
port map (
Clk => Clk,
Reset => Reset,
X => InputA,
Scale => signed(Control0(15 downto 0)),
Offset => signed(Control1(15 downto 0)),
Z => OutputA,
Valid => '1',
OutValid => open
);
-- If you want to change the range of the scale (e.g. multiply by more than 1), then set the
-- NORMAL_SHIFT generic. This increases the range of Scale by 2^N, so NORMAL_SHIFT=4 means that
-- the 16 bit scale here now covers the range -16 to 16.
DSP_RANGE: ScaleOffset
generic map (
NORMAL_SHIFT => 4
)
port map (
Clk => Clk,
Reset => Reset,
X => InputB,
Scale => signed(Control2(15 downto 0)),
Offset => signed(Control3(15 downto 0)),
Z => OutputB,
Valid => '1',
OutValid => open
);
end architecture;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44