Skip to content

Commit

Permalink
Logic has been replaced by triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
oVadim committed Feb 3, 2025
1 parent 14c6a71 commit 8bb89b9
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 91 deletions.
143 changes: 76 additions & 67 deletions labs/2_graphics/2_10_color_shapes_and_functions/lab_top.sv
Original file line number Diff line number Diff line change
Expand Up @@ -74,95 +74,104 @@ module lab_top
assign green = rgb [(( w_green + w_blue) - 1) -: w_green];
assign blue = rgb [ 0 +: w_blue ];

always_comb
always_ff @ (posedge clk or posedge rst)
begin
casex (color)
16'b1???????????????: rgb = {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b0}}};
16'b?1??????????????: rgb = {{w_red {1'b0}}, {w_green {1'b1}}, {w_blue {1'b0}}};
16'b??1?????????????: rgb = {{w_red {1'b0}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b???1????????????: rgb = {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b????1???????????: rgb = {{w_red {1'b1}}, {w_green {1'b1}}, {w_blue {1'b0}}};
16'b?????1??????????: rgb = {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b??????1?????????: rgb = {{w_red {1'b0}}, {w_green {1'b1}}, {w_blue {1'b1}}};
16'b???????1????????: rgb = {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b0}}};
16'b????????1???????: rgb = {{w_red {1'b0}}, {w_green {1'b1}}, {w_blue {1'b0}}};
16'b?????????1??????: rgb = {{w_red {1'b0}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b??????????1?????: rgb = {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b0}}};
16'b???????????1????: rgb = {{w_red {1'b0}}, {w_green {1'b1}}, {w_blue {1'b0}}};
16'b????????????1???: rgb = {{w_red {1'b0}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b?????????????1??: rgb = {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b??????????????1?: rgb = {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b0}}};
16'b???????????????1: rgb = {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b1}}};
default: rgb = {{w_red-1{1'b0}}, 1'b1, {w_green-1{1'b0}}, 1'b1, {w_blue-1{1'b0}}, 1'b1};
endcase
color [15] = paint (x, y, 30, 30, 50, 50, 0); // coordinates of the shapes
color [14] = paint (x, y, 100, 70, 50, 50, 4); // mx, my, rx, ry, shape
color [13] = paint (x, y, 330, 30, 80, 80, 2);
color [12] = paint (x, y, 330, 330, 40, 40, 4); // my
color [11] = paint (x, y, 130, 130, 120, 120, 4); // |
color [10] = paint (x, y, 230, 230, 50, 50, 0); // _V_____________
color [09] = paint (x, y, 330, 330, 50, 50, 0); // mx ->| ^ |
color [08] = paint (x, y, 430, 230, 50, 50, 0); // | | |
color [07] = paint (x, y, 100, 330, 370, 10, 0); // | ry |
color [06] = paint (x, y, 630, 230, 50, 50, 3); // |<- rx -+ |
color [05] = paint (x, y, 650, 30, 50, 50, 1); // | |
color [04] = paint (x, y, 430, 130, 70, 70, 4); // | shape 0 square|
color [03] = paint (x, y, 530, 130, 50, 50, 1); // |_______________|
color [02] = paint (x, y, 130, 30, 50, 50, 1);
color [01] = paint (x, y, 30, 230, 80, 80, 4); // shape 1 rhomb, 2 3^ eye
color [00] = paint (x, y, 530, 330, 60, 60, 4); // > 3 circle
if (rst)
rgb = '0;
else
begin

color [15] <= paint (x, y, 30, 30, 50, 50, 0); // coordinates of the shapes
color [14] <= paint (x, y, 100, 70, 50, 50, 4); // mx, my, rx, ry, shape
color [13] <= paint (x, y, 330, 30, 80, 80, 2);
color [12] <= paint (x, y, 330, 330, 40, 40, 4); // my
color [11] <= paint (x, y, 130, 130, 120, 120, 4); // |
color [10] <= paint (x, y, 230, 230, 50, 50, 0); // _V_____________
color [09] <= paint (x, y, 330, 330, 50, 50, 0); // mx ->| ^ |
color [08] <= paint (x, y, 430, 230, 50, 50, 0); // | | |
color [07] <= paint (x, y, 100, 330, 370, 10, 0); // | ry |
color [06] <= paint (x, y, 630, 230, 50, 50, 3); // |<- rx -+ |
color [05] <= paint (x, y, 650, 30, 50, 50, 1); // | |
color [04] <= paint (x, y, 430, 130, 70, 70, 4); // | shape 0 square|
color [03] <= paint (x, y, 530, 130, 50, 50, 1); // |_______________|
color [02] <= paint (x, y, 130, 30, 50, 50, 1);
color [01] <= paint (x, y, 30, 230, 80, 80, 4); // shape 1 rhomb, 2 3^ eye
color [00] <= paint (x, y, 530, 330, 60, 60, 4); // > 3 circle

casex (color)
16'b1???????????????: rgb <= {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b0}}};
16'b?1??????????????: rgb <= {{w_red {1'b0}}, {w_green {1'b1}}, {w_blue {1'b0}}};
16'b??1?????????????: rgb <= {{w_red {1'b0}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b???1????????????: rgb <= {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b????1???????????: rgb <= {{w_red {1'b1}}, {w_green {1'b1}}, {w_blue {1'b0}}};
16'b?????1??????????: rgb <= {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b??????1?????????: rgb <= {{w_red {1'b0}}, {w_green {1'b1}}, {w_blue {1'b1}}};
16'b???????1????????: rgb <= {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b0}}};
16'b????????1???????: rgb <= {{w_red {1'b0}}, {w_green {1'b1}}, {w_blue {1'b0}}};
16'b?????????1??????: rgb <= {{w_red {1'b0}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b??????????1?????: rgb <= {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b0}}};
16'b???????????1????: rgb <= {{w_red {1'b0}}, {w_green {1'b1}}, {w_blue {1'b0}}};
16'b????????????1???: rgb <= {{w_red {1'b0}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b?????????????1??: rgb <= {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b1}}};
16'b??????????????1?: rgb <= {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b0}}};
16'b???????????????1: rgb <= {{w_red {1'b1}}, {w_green {1'b0}}, {w_blue {1'b1}}};
default: rgb = {{w_red-1{1'b0}}, 1'b1, {w_green-1{1'b0}}, 1'b1, {w_blue-1{1'b0}}, 1'b1};
endcase

end
end

//------------------------------------------------------------------------

// draw a shape when the pixel is inside the intersection of the planes

function automatic logic [0:0] paint (input [9:0] x, y, mx, my, rx, ry, shape);
function automatic logic [0:0] paint (input [9:0] x, y, mx, my, rx, ry, shape);

case (shape)
0: paint = ((t (x, mx, rx)) && (t (y, my, ry))); // square
1: paint = ((t (x, mx, rx)) >= (ry - (t (y, my, ry))) && // rhomb
((t (x, mx, rx)) && (t (y, my, ry))));
2: paint = ((s ((t (x, mx, rx)), rx)) >= (ry - (t (y, my, ry)))) &&
(s ((t (x, mx, rx)), rx)) && (s ((t (y, my, ry)), ry)); // eye
3: paint = ((t (x, mx, rx)) >= (ry - (s ((t (y, my, ry)), ry)))) &&
(s ((t (x, mx, rx)), rx)) && (s ((t (y, my, ry)), ry)); // eye vert
default: paint = ((s ((t (x, mx, rx)), rx)) >= (ry - (s ((t (y, my, ry)), ry)))) &&
(s ((t (x, mx, rx)), rx)) && (s ((t (y, my, ry)), ry)); // circle
0: paint = ((t (x, mx, rx)) && (t (y, my, ry))); // square
1: paint = ((t (x, mx, rx)) > (ry - (t (y, my, ry))) && // rhomb
((t (x, mx, rx)) && (t (y, my, ry))));
2: paint = ((s ((t (x, mx, rx)), rx)) > (ry - (t (y, my, ry)))) &&
(s ((t (x, mx, rx)), rx)) && (s ((t (y, my, ry)), ry)); // eye
3: paint = ((t (x, mx, rx)) > (ry - (s ((t (y, my, ry)), ry)))) &&
(s ((t (x, mx, rx)), rx)) && (s ((t (y, my, ry)), ry)); // eye vert
default: paint = ((s ((t (x, mx, rx)), rx)) > (ry - (s ((t (y, my, ry)), ry)))) &&
(s ((t (x, mx, rx)), rx)) && (s ((t (y, my, ry)), ry)); // circle
endcase

endfunction // paint
endfunction // paint

//------------------------------------------------------------------------

// for triangle, two planes bent at an angle of 90 degrees

function automatic logic [9:0] t (input [9:0] z, m, r);
function automatic logic [9:0] t (input [9:0] z, m, r);

if ((z >= m) && (z < (m + r)))
t = z - m;
else if ((z >= m + r) && (z <= (m + (r << 1))))
t = m - z + (r << 1);
else
t = '0;
if ((z >= m) && (z < (m + r)))
t = z - m;
else if ((z >= m + r) && (z <= (m + (r << 1))))
t = m - z + (r << 1);
else
t = '0;

endfunction // triangle
endfunction // triangle

//------------------------------------------------------------------------

// for sinus from triangle, the plane bent around the sine envelope

function automatic logic [9:0] s (input [9:0] t, r);
function automatic logic [9:0] s (input [9:0] t, r);

if (t < (r >> 1) - (r >> 4)) // < 0.4375 r
s = t + (t >> 1) - (t >> 5); // + 1.46875 t
else if (t < (r >> 1) + (r >> 3)) // < 0.625 r
s = t - (t >> 3) + (r >> 2); // 0.25 r + 0.875 t
else if (t < (r >> 1) + (r >> 2) + (r >> 4)) // < 0.8125 r
s = (t >> 1) + (t >> 5) + (r >> 1) - (r >> 5);
else // 0.46875 r + 0.53125 t
s = (t >> 3) + (t >> 4) + r - (r >> 2);
// 0.75 r + 0.1875 t

if (t < (r >> 1) - (r >> 4)) // < 0.4375 r
s = t + (t >> 1) - (t >> 5); // + 1.46875 t
else if (t < (r >> 1) + (r >> 3)) // < 0.625 r
s = t - (t >> 3) + (r >> 2); // 0.25 r + 0.875 t
else if (t < (r >> 1) + (r >> 2) + (r >> 4)) // < 0.8125 r
s = (t >> 1) + (t >> 5) + (r >> 1) - (r >> 5);
else // 0.46875 r + 0.53125 t
s = (t >> 3) + (t >> 4) + r - (r >> 2) - (r >> 5);
// 0.71875 r + 0.1875 t
endfunction // sinus
endfunction // sinus

endmodule
endmodule
4 changes: 2 additions & 2 deletions labs/3_music/3_8_waveform_generator/lab_top.sv
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module lab_top
// Microphone, sound output and UART

input [ 23:0] mic,
output [w_sound - 1:0] sound,
output logic [w_sound - 1:0] sound,

input uart_rx,
output uart_tx,
Expand Down Expand Up @@ -94,7 +94,7 @@ module lab_top
i_waveform_gen
(
.clk ( clk ),
.reset ( rst ),
.rst ( rst ),
.octave ( octave ),
.waveform ( waveform ),

Expand Down
8 changes: 6 additions & 2 deletions labs/3_music/3_8_waveform_generator/oscilloscope.sv
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ module oscilloscope
wire [w_x-1:0] cntx = counter [18-:w_x];
wire cntx_in_buf = cntx < screen_width / 2;

assign white = x <= vldx
always_ff @ (posedge clk or posedge rst)
if (rst)
white <= '0;
else
white <= (x <= vldx
&& (x >> 2) < (distance [18-:w_x])
&& (y >> 3) == (midy - bufy [(x >> 2)]) >> 3
&& x < screen_width && y < screen_height;
&& x < screen_width && y < screen_height);

always_ff @ (posedge clk)
if (cntx_in_buf)
Expand Down
48 changes: 28 additions & 20 deletions labs/3_music/3_8_waveform_generator/waveform_gen.sv
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module waveform_gen
)
(
input clk,
input reset,
input rst,
input [ 2:0] octave,
input [ 3:0] waveform, // waveform type
output logic [y_width - 1:0] y
Expand All @@ -35,8 +35,8 @@ module waveform_gen
logic [y_width - 1:0] ys;
logic [y_width - 1:0] yq;

always_ff @ (posedge clk or posedge reset)
if (reset)
always_ff @ (posedge clk or posedge rst)
if (rst)
clk_div <= '0;
else
clk_div <= clk_div + 1'b1;
Expand All @@ -63,19 +63,25 @@ module waveform_gen
//
//------------------------------------------------------------------------

always_comb begin
always_ff @ (posedge clk or posedge rst)
begin
if (rst)
y <= '0;
else
case (waveform)
'b0001: y = ys; // sinus
'b0010: y = yt; // triangle
'b0100: y = yq; // square
default: y = '0;
'b0001: y <= ys; // sinus
'b0010: y <= yt; // triangle
'b0100: y <= yq; // square
default: y <= '0;
endcase
end

//------------------------------------------------------------------------

sinus i_sinus
(
.clk ( clk ),
.rst ( rst ),
.y_max ( y_max ),
.yt ( yt ),
.ys ( ys )
Expand All @@ -100,6 +106,8 @@ endmodule

module sinus
(
input clk,
input rst,
input logic [15:0] y_max,
input logic [15:0] yt,
output logic [15:0] ys
Expand All @@ -112,29 +120,29 @@ module sinus
// decreasing transmission ratio as the level of the triangle
// moves toward the two extremes. Accuracy of the sine is 1%.

always_comb
always_ff @ (posedge clk)
begin
if (yt > (MAX >> 1)) // Shifting to right >> 1 for positive numbers is equivalent to dividing by 2
begin
if (yt > MAX - (y_max >> 1) + (y_max >> 4)) // negative half-wave
ys = yt - ((MAX - yt) >> 1) + ((MAX - yt) >> 5);
ys <= yt - ((MAX - yt) >> 1) + ((MAX - yt) >> 5);
else if (yt > (MAX - (y_max >> 1) - (y_max >> 3)))
ys = yt + ((MAX - yt) >> 4) - (y_max >> 2);
ys <= yt + ((MAX - yt) >> 4) - (y_max >> 2);
else if (yt > MAX - (y_max >> 1) - (y_max >> 2) - (y_max >> 4))
ys = MAX - ((MAX - yt) >> 1) - ((MAX - yt) >> 4) - (y_max >> 1);
ys <= MAX - ((MAX - yt) >> 1) - ((MAX - yt) >> 4) - (y_max >> 1);
else
ys = MAX - ((MAX - yt) >> 3) - ((MAX - yt) >> 5) - y_max + (y_max >> 3) + (y_max >> 5);
ys <= MAX - ((MAX - yt) >> 3) - ((MAX - yt) >> 5) - y_max + (y_max >> 3) + (y_max >> 5);
end
else
begin
if (yt < (y_max >> 1) - (y_max >> 4)) // < 0.4375 y_max
ys = yt + (yt >> 1) - (yt >> 5); // + 1.46875 yt
else if (yt < (y_max >> 1) + (y_max >> 3)) // < 0.625 y_max
ys = yt - (yt >> 4) + (y_max >> 2); // 0.25 y_max + 0.9375 yt
else if (yt < (y_max >> 1) + (y_max >> 2) + (y_max >> 4)) // < 0.8125 y_max
ys = (yt >> 1) + (yt >> 4) + (y_max >> 1); // 0.5 y_max + 0.5625 yt
if (yt < (y_max >> 1) - (y_max >> 4)) // < 0.4375 y_max
ys <= yt + (yt >> 1) - (yt >> 5); // + 1.46875 yt
else if (yt < (y_max >> 1) + (y_max >> 3)) // < 0.625 y_max
ys <= yt - (yt >> 4) + (y_max >> 2); // 0.25 y_max + 0.9375 yt
else if (yt < (y_max >> 1) + (y_max >> 2) + (y_max >> 4)) // < 0.8125 y_max
ys <= (yt >> 1) + (yt >> 4) + (y_max >> 1); // 0.5 y_max + 0.5625 yt
else
ys = (yt >> 3) + (yt >> 5) + y_max - (y_max >> 3) - (y_max >> 5); // 0.84375 y_max + 0.15625 yt
ys <= (yt >> 3) + (yt >> 5) + y_max - (y_max >> 3) - (y_max >> 5); // 0.84375 y_max + 0.15625 yt
end
end

Expand Down

0 comments on commit 8bb89b9

Please sign in to comment.