reg clk = 1'b0;
reg rst = 1'b1;
reg start, finish;
reg [7:0] din;
reg din_valid;
wire din_ready;
reg din_end;
wire [7:0] dout;
wire dout_valid;
wire dout_end;
// Assert when done to finish the simulation.
reg done = 1'b0;
// Clock driver.
localparam CLK_PD = 10;
always @clk #(CLK_PD/2) clk <= ~clk & ~done;
bloke2b uut (
.clk(clk),
.rst(rst),
.start(start),
.finish(finish),
.din(din),
.din_valid(din_valid),
.din_ready(din_ready),
.din_end(din_end),
.dout(dout),
.dout_valid(dout_valid),
.dout_end(dout_end)
);
// Monitor for the message received from the UUT.
reg [511:0] message;
always @(posedge clk) begin
// Very noisy diagnostic, but useful
//$display("%t tb dv %b de %b m %h", $time, dout_valid, dout_end, message);
// If we're starting, clear the message contents.
if (start) message = "";
// If we have valid data out, shift it in.
if (dout_valid) message = {message[503:0], dout};
// If the data finished, display what we received. It's a hash, so it's
// probably garbage, but you never know...
if (dout_end) $display("Received message: %s", message);
end
// Run a string through the hash machine.
task hash_message (
input [1023:0] msg
);
integer i, len;
begin
// Ugh, working around a bug in old Icarus Verilog 10.x which
// prevents the use of proper strings here. Get the string
// length because the string literal comes in backwards.
for (i = 0; msg[8*i +: 8] != 0; i++); len = i;
// Set our start and finish lines correctly.
start <= 1'b1;
finish <= 1'b0;
@(posedge clk);
start <= 1'b0;
finish <= 1'b0;
// Wait until it signals readiness.
while (!din_ready) @(posedge clk);
// Write it in byte by byte when drdy is asserted.
for (i = len - 1; i >= 0; i--) begin
din <= msg[8*i +: 8];
din_valid <= 1'b1;
do @(posedge clk); while (!din_ready);
end
// Deassert data valid, since we're done.
din_valid <= 1'b0;
// And assert finish for a clock.
finish <= 1'b1;
@(posedge clk);
finish <= 1'b0;
// Wait until the message processes.
@(negedge dout_end);
end endtask
initial begin
// Initialize our start and finish lines.
start <= 1'b0;
finish <= 1'b0;
// Kick off the clock, since it needs an event to start.
clk <= 1'b1;
// Wait for the next clock edge and pull the reset.
@(posedge clk);
rst <= 1'b0;
// Run a few tests.
hash_message("");
hash_message("123");
hash_message("abc");
// And we're done, stop the clock.
done <= 1'b1;
end
reg clk = 1'b0;
reg rst = 1'b1;
reg start, finish;
reg [7:0] din;
reg din_valid;
wire din_ready;
reg din_end;
wire [7:0] dout;
wire dout_valid;
wire dout_end;
// Assert when done to finish the simulation.
reg done = 1'b0;
// Clock driver.
localparam CLK_PD = 10;
always @clk #(CLK_PD/2) clk <= ~clk & ~done;
bloke2s uut (
.clk(clk),
.rst(rst),
.start(start),
.finish(finish),
.din(din),
.din_valid(din_valid),
.din_ready(din_ready),
.din_end(din_end),
.dout(dout),
.dout_valid(dout_valid),
.dout_end(dout_end)
);
// Monitor for the message received from the UUT.
reg [511:0] message;
always @(posedge clk) begin
// Very noisy diagnostic, but useful
//$display("%t tb dv %b de %b m %h", $time, dout_valid, dout_end, message);
// If we're starting, clear the message contents.
if (start) message = "";
// If we have valid data out, shift it in.
if (dout_valid) message = {message[503:0], dout};
// If the data finished, display what we received. It's a hash, so it's
// probably garbage, but you never know...
if (dout_end) $display("Received message: %s", message);
end
// Run a string through the hash machine.
task hash_message (
input [1023:0] msg
);
integer i, len;
begin
// Ugh, working around a bug in old Icarus Verilog 10.x which
// prevents the use of proper strings here. Get the string
// length because the string literal comes in backwards.
for (i = 0; msg[8*i +: 8] != 0; i++); len = i;
// Set our start and finish lines correctly.
start <= 1'b1;
finish <= 1'b0;
@(posedge clk);
start <= 1'b0;
finish <= 1'b0;
// Wait until it signals readiness.
while (!din_ready) @(posedge clk);
// Write it in byte by byte when drdy is asserted.
for (i = len - 1; i >= 0; i--) begin
din <= msg[8*i +: 8];
din_valid <= 1'b1;
do @(posedge clk); while (!din_ready);
end
// Deassert data valid, since we're done.
din_valid <= 1'b0;
// And assert finish for a clock.
finish <= 1'b1;
@(posedge clk);
finish <= 1'b0;
// Wait until the message processes.
@(negedge dout_end);
end endtask
initial begin
// Initialize our start and finish lines.
start <= 1'b0;
finish <= 1'b0;
// Kick off the clock, since it needs an event to start.
clk <= 1'b1;
// Wait for the next clock edge and pull the reset.
@(posedge clk);
rst <= 1'b0;
// Run a few tests.
hash_message("");
hash_message("123");
hash_message("abc");
// And we're done, stop the clock.
done <= 1'b1;
end
localparam MSG_BITS=W*16;
reg [MSG_BITS-1:0] m;
assign m_out = m;
reg [W*2-1:0] t;
assign t_out = {t[0 +: W], t[W +: W]};
reg f;
assign f_out = f;
reg tst;
localparam CNT_BITS=$clog2(W*16/8);
reg [CNT_BITS-1:0] cnt;
wire [CNT_BITS:0] next_cnt = cnt + 1;
wire carry = next_cnt[CNT_BITS];
assign msg_strobe = (carry & dv_in) | (finish & ~f & ~start);
always @(posedge clk) begin
if (rst | start) begin
m <= {MSG_BITS{1'b0}};
cnt <= {CNT_BITS{1'b0}};
t <= {(W*2){1'b0}};
f <= 1'b0;
tst <= finish;
end else begin
if (dv_in) begin
m[((W-cnt)*8) +: 8] <= data_in;
cnt <= next_cnt[CNT_BITS-1:0];
t <= t + 1;
f <= finish;
//$display("%t dmgr din d %h m %h c %h t %h f %b t %b", $time, data_in, m, cnt, t, f, tst);
end else if (finish) begin
f <= 1'b1;
end
end
end
localparam TEST_VAL = 512'h3c9cf0addf2e45ef548b011f736cc99144bdfee0d69df4090c8a39c520e18ec3bdc1277aad1706f756affca41178dac066e4beb8ab7dd2d1402c4d624aaabe40;
reg [(W*8)-1:0] h;
reg [$clog2(W):0] out_cnt;
assign data_out = h[7:0];
assign dv_out = (out_cnt != 0);
assign data_end = (out_cnt == 1);
always @(posedge clk) begin
if (rst) begin
out_cnt <= 0;
end else begin
//$display("%t dmgr dout oc %h", $time, out_cnt);
if (h_rdy) begin
//$display("%t dmgr dout h %h t %b", $time, h_in, tst);
out_cnt <= W;
h <= h_in ^ (TEST_VAL & {(W*16){tst}});
end else if(out_cnt != 0) begin
//$display("%t dmgr dout d %h dv %b de %b oc %h", $time, data_out, dv_out, data_end, out_cnt);
out_cnt <= out_cnt - 1;
h <= {8'b0, h[W*8-1:8]};
end
end
end
To extract the secret message embedded in this Verilog code, you need to trigger a specific condition in the data_mgr module that causes it to output the hidden message. Here's how you can do it:
Modify the Testbenches:
In both bloke2b_tb and bloke2s_tb, you need to modify the hash_message task to set both the start and finish signals to 1 simultaneously during the start of the hashing process. This is the key to triggering the secret message.
Here's How to Do It:
Locate the hash_message Task:
In both testbenches (bloke2b_tb and bloke2s_tb), find the hash_message task. It looks like this:
task hash_message (
input [1023:0] msg
);
integer i, len;
begin
// Ugh, working around a bug in old Icarus Verilog 10.x which
// prevents the use of proper strings here. Get the string
// length because the string literal comes in backwards.
for (i = 0; msg[8*i +: 8] != 0; i++); len = i;
// Set our start and finish lines correctly.
start <= 1'b1;
finish <= 1'b0;
@(posedge clk);
start <= 1'b0;
finish <= 1'b0;
// Rest of the code...
end
Modify the start and finish Signals:
Change the lines where start and finish are assigned during the start of the task:
start <= 1'b1;
finish <= 1'b0;
to:
start <= 1'b1;
finish <= 1'b1; // Set finish to 1 to trigger the secret message
Explanation:
By setting both start and finish to 1 at the same time, you cause the state machine in the bloke2 module to enter the STATE_TEST state. This state is responsible for setting a flag (tst) in the data_mgr module.
When tst is 1, the data_mgr module modifies the output hash by XORing it with a predefined TEST_VAL, which contains the secret message.
if (h_rdy) begin
out_cnt <= W;
h <= h_in ^ (TEST_VAL & {(W*16){tst}});
end
Since tst is 1, the hash output (h) becomes h_in XOR TEST_VAL, effectively revealing the secret message when you capture the output.
Run the Simulation:
After modifying the testbenches, rerun the simulation. The testbenches are designed to collect the output bytes and display them as a string when the output data ends.
In the testbenches, the output is collected in a message variable and printed:
// If we have valid data out, shift it in.
if (dout_valid) message = {message[503:0], dout};
// If the data finished, display what we received.
if (dout_end) $display("Received message: %s", message);
With the modifications, when you simulate the testbenches, they will print out the secret message stored in TEST_VAL.
Example:
After making the changes and running the simulation, you might see output like:
Received message: Secret Message Hidden Here
Conclusion:
By setting both start and finish to 1 at the beginning of the hashing process in the testbenches, you trigger the code that reveals the secret message.
Note: The actual message will be whatever is stored in TEST_VAL in the data_mgr module. The above example output is illustrative. When you run the simulation with the modified testbenches, you will see the actual secret message.
Answer: By modifying the testbenches to set both start and finish to 1 when starting—causing tst to be set—so the secret message is output; that is, trigger it by starting with start and finish both set to 1