Below is a verilog program. There is a secret message that can be extracted with the testbenches. I know that the secret message contains substring "@flare-on.com". How can I trigger it?
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, you need to trigger the code to output different data under special conditions where both the start and finish signals are asserted simultaneously. The Verilog code has a built-in mechanism to reveal a secret when start and finish are both high.
Here's how you can do it:
Modify the Testbench to Assert start and finish Together:
In the testbench (either bloke2b_tb or bloke2s_tb), modify the hash_message task to assert both start and finish at the same time. Specifically, change the initial assertion of finish to be simultaneous with start. Here's the modified hash_message task:
verilog
task hash_message (
input [1023:0] msg
);
integer i, len;
begin
// Get the string length
for (i = 0; msg[8*i +: 8] != 0; i++); len = i;
// Set both start and finish lines
start <= 1'b1;
finish <= 1'b1;
@(posedge clk);
start <= 1'b0;
finish <= 1'b0;
// Wait until it signals readiness
while (!din_ready) @(posedge clk);
// Write the message byte by byte
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
din_valid <= 1'b0;
// Wait for the message to process
@(negedge dout_end);
end endtask
Run the Testbench Twice with the Same Message:
First Run (Normal Operation): Run the testbench without the above modification (i.e., with start and finish not asserted together). Record the output message.
Second Run (Secret Triggered): Run the testbench with the modification (with start and finish asserted together). Record the output message.
Compare the Outputs to Extract the Secret:
XOR the outputs from the two runs. Since the code uses an XOR operation with a TEST_VAL when both start and finish are asserted, XORing the two outputs will cancel out the common parts and reveal TEST_VAL, which contains the secret message.
verilog
// Pseudocode for comparing outputs
secret_output = output_with_both_asserted ^ output_normal;
Extract and Interpret the Secret Message:
The resulting secret_output will contain the secret message, which includes the substring @flare-on.com. You can interpret this output as ASCII characters to read the secret message.
Explanation of Why This Works:
The data_mgr module in the code has a parameter TEST_VAL. When both start and finish are asserted together (setting tst to 1), the output h becomes h_in ^ TEST_VAL.
verilog
h <= h_in ^ (TEST_VAL & {(W*16){tst}});
By running the test with and without the simultaneous assertion, you get h_in and h_in ^ TEST_VAL. XORing these two outputs gives you TEST_VAL.
The TEST_VAL is a hardcoded value in the code that contains the secret message when interpreted correctly.
Note: Ensure you're complying with any licensing or usage restrictions when modifying and running the code.