add program machine code as parameter to ROM and top_level,

change output of example assembler scripts,
update testbenches for programs 1 2 3 to use overloadable parameter,
formatted program3_tb
This commit is contained in:
Arthur Lu 2022-08-30 19:10:38 -07:00
parent 155d18721f
commit 4b909d48dd
8 changed files with 170 additions and 179 deletions

View File

@ -2,7 +2,7 @@
// Project Name: CSE141L
// Description: instruction ROM module for use with InstFetch
module InstROM #(parameter A=10, W=9) (
module InstROM #(parameter A=10, W=9, machine_code_file = "machine_code.txt") (
input logic [A-1:0] InstAddress,
output logic[W-1:0] InstOut
);
@ -11,8 +11,9 @@ module InstROM #(parameter A=10, W=9) (
assign InstOut = inst_rom[InstAddress];
// use readmemb to read ascii 0 and 1 representation of binary values from text file
initial begin
$readmemb("machine_code.txt",inst_rom);
$readmemb(machine_code_file,inst_rom);
end
endmodule

View File

@ -63,7 +63,7 @@ module program1_tb ();
end
// ***** instantiate your own top level design here *****
top_level dut(
top_level #(.machine_code_file("machine_code_1.txt")) dut(
.clk(clk), // input: use your own port names, if different
.init(init), // input: some prefer to call this ".reset"
.req(start), // input: launch program

View File

@ -62,7 +62,7 @@ module program2_tb () ;
end
// ***** instantiate your own top level design here *****
top_level dut(
top_level #(.machine_code_file("machine_code_2.txt")) dut(
.clk(clk), // input: use your own port names, if different
.init(init), // input: some prefer to call this ".reset"
.req(start), // input: launch program

View File

@ -4,34 +4,28 @@
// runs program 2 (decrypt a message), but with corruption
module program3_tb ();
// DUT interface
logic clk = 1'b0 , // advances simulation step-by-step
init = 1'b1 , // init (reset) command to DUT
start = 1'b1 ; // req (start program) command to DUT
logic clk = 1'b0; // advances simulation step-by-step
logic init = 1'b1; // init (reset) command to DUT
logic start = 1'b1; // req (start program) command to DUT
wire done; // done flag returned by DUT
// test bench parameters
logic [3:0] pre_length; // space char. bytes before first char. in message
logic[7:0] message1[49] , // original raw message, in binary
msg_padded1[80], // original message, plus pre- and post-padding w/ ASCII spaces
msg_crypto1[64]; // encrypted message according to the DUT
logic[6:0] lfsr_ptrn , // chosen one of 9 maximal length 7-tap shift reg. ptrns
LFSR_ptrn[9] , // the 9 candidate maximal-length 7-bit LFSR tap ptrns
lfsr1[64] , // states of program 1 encrypting LFSR
LFSR_init ; // one of 127 possible NONZERO starting states
logic [7:0] message1[49]; // original raw message, in binary
logic [7:0] msg_padded1[80]; // original message, plus pre- and post-padding w/ ASCII spaces
logic [7:0] msg_crypto1[64]; // encrypted message according to the DUT
logic [6:0] lfsr_ptrn; // chosen one of 9 maximal length 7-tap shift reg. ptrns
logic [6:0] LFSR_ptrn[9]; // the 9 candidate maximal-length 7-bit LFSR tap ptrns
logic [6:0] lfsr1[64]; // states of program 1 encrypting LFSR
logic [6:0] LFSR_init; // one of 127 possible NONZERO starting states
int score; // count of correct encyrpted characters
// our original American Standard Code for Information Interchange message follows
// note in practice your design should be able to handle ANY ASCII string that is
// restricted to characters between space (0x20) and script f (0x9f) and shorter than
// 53 characters in length
// *** No more than 24 leading space characters, including preamble. ***
// string str1 =
string str1 = " four score and seven years ago..."; // sample program 1 input
// string str1 = " Knowledge comes, but wisdom lingers. "; // alternative inputs
// string str1 = " 01234546789abcdefghijklmnopqrstuvwxyz. "; // (make up your own,
// string str1 = " A joke is a very serious thing."; // as well)
// string str1 = " Ajok "; //
// string str1 = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
// string str1 = "``@@```@@@````@@@@````@@@@@";
// string str1 = " Knowledge comes, but wisdom lingers. "; //
// displayed encrypted string will go here:
string str_enc1 [64]; // program 1 desired output will go here
@ -51,11 +45,13 @@ module program3_tb () ;
assign LFSR_ptrn[6] = 7'h5C;
assign LFSR_ptrn[7] = 7'h7E;
assign LFSR_ptrn[8] = 7'h7B;
always_comb begin
pt_no = $urandom_range(0, 8); //$random>>22; // or pick a specific one
if(pt_no>8) pt_no[3] = 1'b0; // restrict to 0 through 8 (our legal patterns)
lfsr_ptrn = LFSR_ptrn[pt_no]; // engage the selected pattern
end
// now select a starting LFSR state -- any nonzero value will do
always_comb begin
LFSR_init = $urandom;//$random>>2; // or set a value, such as 7'b1, for debug
@ -70,7 +66,7 @@ module program3_tb () ;
end
// ***** instantiate your own top level design here *****
top_level dut(
top_level #(.machine_code_file("machine_code_3.txt")) dut(
.clk(clk), // input: use your own port names, if different
.init(init), // input: some prefer to call this ".reset"
.req(start), // input: launch program
@ -78,13 +74,7 @@ module program3_tb () ;
);
initial begin
//***** pre-load your instruction ROM here or inside itself *****
// $readmemb("encoder.bin", dut.instr_rom.rom);
// you may also pre-load desired constants, etc. into
// your data_mem here -- the upper addresses are reserved for your use
// dut.data_mem.DM[128]=8'hfe; //whatever constants you want
file_no = 1; // write to transcript
// file_no = $fopen("msg_decoder_out.txt"); // create your output file
file_no = 1;
#0ns strlen = str1.len; // length of string 1 (# characters between " ")
if(strlen>52) strlen = 52; // clip message at 52 characters
for(space=0;space<24;space++) // count leading spaces in message
@ -99,7 +89,7 @@ module program3_tb () ;
// will subtract 0x20 from each preamble and messge character to fit into 7 bits
for(int j=0; j<80; j++) // pre-fill message_padded with ASCII space characters
msg_padded1[j] = 8'h20; //
msg_padded1[j] = 8'h20;
for(int l=0; l<strlen; l++) // overwrite up to 49 of these spaces w/ message itself
msg_padded1[l+pre_length] = str1[l];
// compute the LFSR sequence
@ -111,8 +101,7 @@ module program3_tb () ;
for (int i=0; i<64; i++) begin
msg_crypto1[i] = ((msg_padded1[i]-'h20) ^ lfsr1[i]);
msg_crypto1[i][7] = ^msg_crypto1[i][6:0]; // prepend parity bit into MSB
$fdisplay(file_no,"i=%d, msg_pad=0x%h, lfsr=%b msg_crypt w/ parity = 0x%h",
i,msg_padded1[i],lfsr1[i],msg_crypto1[i]);
$fdisplay(file_no,"i=%d, msg_pad=0x%h, lfsr=%b msg_crypt w/ parity = 0x%h", i,msg_padded1[i],lfsr1[i],msg_crypto1[i]);
//for display purposes only, add 8'h20 to avoid nonprintable characters
str_enc1[i] = string'(msg_crypto1[i][6:0]+'h20);
end
@ -134,17 +123,18 @@ module program3_tb () ;
for(int m=0; m<24; m++) // load first 24 characters of encrypted message into data memory
dut.DM.core[m+64] = msg_crypto1[m];
for(int n=24; n<64; n++) begin // load subsequent, possibly corrupt, encrypted message into data memory
// set flipper = 8 or higher to disable bit corruption
flipper = $random;//$random; // value between 0 and 63, inclusive
flipper = $urandom_range(0, 63); // value between 0 and 63, inclusive
dut.DM.core[n+64] = msg_crypto1[n]^(1<<flipper);
if(flipper<8) flipped[n]=1;
end
#20ns init = 1'b0; // suggestion: reset = 1 forces your program counter to 0
#10ns start = 1'b0; // request/start = 1 holds your program counter
#60ns; // wait for 6 clock cycles of nominal 10ns each
wait(done); // wait for DUT's ack/done flag to go high
wait(done); // wait for DUT's ack/done flag to go high\
#10ns $fdisplay(file_no,"");
$fdisplay(file_no,"program 3:");
// ***** reads your results and compares to test bench
// ***** use your instance name for data memory and its internal core *****
for(int n=0; n<(64-pre_length-space); n++) begin
@ -156,16 +146,16 @@ module program3_tb () ;
$fdisplay(file_no, "failed to flag error");
end
end
// else if({flipped[n+pre_length+space],msg_padded1[n+pre_length+space][6:0]}
else if(msg_padded1[n+pre_length+space] == dut.DM.core[n]+32) begin
$fdisplay(file_no,"%d bench msg: %s %h dut msg: %h",
n, msg_padded1[n+pre_length+space][6:0], msg_padded1[n+pre_length+space], dut.DM.core[n]);
$fdisplay(file_no,"%d bench msg: %s %h dut msg: %h", n, msg_padded1[n+pre_length+space][6:0], msg_padded1[n+pre_length+space], dut.DM.core[n]);
score++;
end
else
else begin
$fdisplay(file_no,"%d bench msg: %s %h dut msg: %h OOPS!",
n, msg_padded1[n+pre_length+space][6:0], msg_padded1[n+pre_length+space], dut.DM.core[n]);
end
end
$fdisplay(file_no,"score = %0d/%0d",score,64-pre_length-space);
#20ns $fclose(file_no);
#20ns $stop;

View File

@ -4,7 +4,7 @@
import Definitions::*;
module top_level(
module top_level #(parameter machine_code_file = "machine_code.txt") (
input clk, init, req,
output logic ack
);
@ -72,7 +72,7 @@ module top_level(
.DataOut(mem_out)
);
InstROM rom (
InstROM #(.machine_code_file(machine_code_file)) rom (
.InstAddress(ProgCtr),
.InstOut(Instruction)
);

View File

@ -1,2 +1,2 @@
#!/bin/sh
python assembler.py machine_code.txt program1.asm
python assembler.py machine_code_1.txt program1.asm

View File

@ -1,2 +1,2 @@
#!/bin/sh
python assembler.py machine_code.txt program2.asm
python assembler.py machine_code_2.txt program2.asm

View File

@ -1,2 +1,2 @@
#!/bin/sh
python assembler.py machine_code.txt program3.asm
python assembler.py machine_code_3.txt program3.asm