diff --git a/RTL/program1_tb.sv b/RTL/program1_tb.sv new file mode 100644 index 0000000..ec631f0 --- /dev/null +++ b/RTL/program1_tb.sv @@ -0,0 +1,149 @@ +// program1_tb +// testbench for programmable message encryption (Program #1) +// CSE141L +// runs program 1 (encrypt a message) +module program1_tb (); + // DUT interface -- four one-bit wires, three to DUT, one from + bit clk; // advances simulation step-by-step + bit init = 1'b1; // init (reset) command to DUT + bit start = 1'b1; // request (start program) command to DUT + wire done; // acknowledge (program done) flag returned by DUT + + // test bench parameters + logic [7:0] pre_length; // number of space char. before message itself, sent to data_mem[61] + logic [7:0] message1[54]; // original raw message, in binary, up to 54 characters in length + logic [7:0] msg_padded1[64]; // original message, plus pre- and post-padding w/ ASCII spaces + logic [7:0] msg_crypto1[64]; // encrypted message returned by DUT + logic [7:0] lfsr_ptrn; // choses one of 9 maximal length 7-tap shift reg. patterns + logic [7:0] LFSR_ptrn[9]; // the 9 candidate maximal-length 7-bit LFSR tap ptrns themselves + logic [7:0] lfsr1[64]; // states of program 1 encrypting LFSR + logic [7:0] LFSR_init; // one of 127 possible NONZERO starting states for encrypting LFSR + int score; // count of correctly 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 + // 55 characters in length + string str1 = "Mr. Watson, come here. I want to see you."; + + // displayed encrypted string will go here: + string str_enc1[64]; // program 1 desired output will go here + int strlen; // length of incoming message string itself, before padding + logic [3:0] pt_no; // select LFSR pattern, index value 0 through 8 + int file_no; // output file tag (set to 1 for write to console/transcript) + + // the 9 possible 7-tap maximal-length feedback tap patterns from which to choose + assign LFSR_ptrn[0] = 8'h60; + assign LFSR_ptrn[1] = 8'h48; + assign LFSR_ptrn[2] = 8'h78; + assign LFSR_ptrn[3] = 8'h72; + assign LFSR_ptrn[4] = 8'h6A; + assign LFSR_ptrn[5] = 8'h69; + assign LFSR_ptrn[6] = 8'h5C; + assign LFSR_ptrn[7] = 8'h7E; + assign LFSR_ptrn[8] = 8'h7B; + + always_comb begin + pt_no = $random; + if(pt_no>8) pt_no[3] = 0; // restrict pt_no to 0 through 8 + lfsr_ptrn = LFSR_ptrn[pt_no]; // look up and engage the selected pattern; to data_mem[62] + end + + // now select a starting LFSR state -- any nonzero value will do + always_comb begin + LFSR_init = $random; + if(!LFSR_init) LFSR_init = 7'b1; // prevents illegal starting state = 7'b0; + end + + // set preamble length for the program run (always > 9 but < 26) + always_comb begin + pre_length = $random; + if(pre_length < 10) pre_length = 10; // prevents pre_length < 10 + else if(pre_length > 26) pre_length = 26; // prevets pre_length > 26 + end + + // ***** instantiate your own top level design here ***** + top_level 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 + .ack(done) // output: "program run complete" + ); + + initial begin + file_no = 'b1; + //file_no = $fopen("msg_enocder_out.txt","w"); + #0ns strlen = str1.len; // length of string 1 (# characters between " ") + if(strlen>54) strlen = 54; // clip message at 54 characters + // program 1 -- precompute encrypted message + lfsr1[0] = LFSR_init; // any nonzero value (zero may be helpful for debug) + $fdisplay(file_no,"run encryption program; original message = "); + $fdisplay(file_no,"%s",str1); // print original message in transcript window + $fdisplay(file_no,"pt_no = %d",pt_no); + $fdisplay(file_no,"LFSR_ptrn = 0x%h, LFSR_init = 0x%h",lfsr_ptrn,LFSR_init); + + // will subtract 0x20 from each preamble and each message character + for(int j=0; j<64; j++) // pre-fill message_padded with ASCII space characters + msg_padded1[j] = 8'h20; // + for(int l=0; l8) pt_no[3] = 0; // restrict pt_no to 0 through 8 - lfsr_ptrn = LFSR_ptrn[pt_no]; // look up and engage the selected pattern; to data_mem[62] - end -// now select a starting LFSR state -- any nonzero value will do - always_comb begin - LFSR_init = 'b1;//$random>>2; // or set a specific value, such as 7'b1, for easier debug - if(!LFSR_init) LFSR_init = 7'b1; // prevents illegal starting state = 7'b0; - end - -// set preamble length for the program run (always > 9 but < 26) - always_comb begin - pre_length = 10;//$random>>10 ; // number of space characters ahead of message; the >>10 changes the random value - if(pre_length < 10) pre_length = 10; // prevents pre_length < 10 - else if(pre_length > 26) pre_length = 26; - end - -// ***** instantiate your own top level design here ***** - top_level 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 - .ack (done ) // output: "program run complete" - ); - - 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 -// to display to console, change this line to file_no = 'b1; - file_no = 'b1; // display to console instead of file -// file_no = $fopen("msg_enocder_out.txt","w"); // create your output file - #0ns strlen = str1.len; // length of string 1 (# characters between " ") - if(strlen>54) strlen = 54; // clip message at 54 characters -// program 1 -- precompute encrypted message - lfsr1[0] = LFSR_init; // any nonzero value (zero may be helpful for debug) - $fdisplay(file_no,"run encryption program; original message = "); - $fdisplay(file_no,"%s",str1); // print original message in transcript window - $fdisplay(file_no,"pt_no = %d",pt_no); - $fdisplay(file_no,"LFSR_ptrn = 0x%h, LFSR_init = 0x%h",lfsr_ptrn,LFSR_init); - -// will subtract 0x20 from each preamble and each message character - for(int j=0; j<64; j++) // pre-fill message_padded with ASCII space characters - msg_padded1[j] = 8'h20; // - for(int l=0; l