fix bug in PC jump target calculation,

fix bug in RegFile input width,
fix control logic bug with INC/DEC,
change assembler output format,
change programs to use d0 as space char value
This commit is contained in:
Arthur Lu 2022-08-14 13:44:51 -07:00
parent 0dbd5dbbea
commit 935864ad9c
11 changed files with 113 additions and 105 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
**/*.bin
**/machine_code.txt

View File

@ -11,12 +11,13 @@ module Ctrl #(
input logic [8:0] Instruction,
input logic [W-1:0] ALU_Out,
input logic [W-1:0] RegOutA, RegOutB, // select from register inputs or immediate inputs
input logic [T-1:0] ProgCtr_p4,
input logic [T-1:0] ProgCtr_p1,
input logic [W-1:0] mem_out,
output op_mne ALU_OP, // control ALU operation
output logic [W-1:0] ALU_A, ALU_B,
output logic RegWrite, Done_in,
output logic [3:0] RaddrA, RaddrB, Waddr, RegInput,
output logic [3:0] RaddrA, RaddrB, Waddr,
output logic [W-1:0] RegInput,
output logic BranchEZ, BranchNZ, BranchAlways,
output logic write_mem
);
@ -50,90 +51,90 @@ module Ctrl #(
BranchAlways = 'b0;
write_mem = 'b0;
casez(Instruction)
'b1_xxxx_xxxx: begin // LDI
'b1_????_????: begin // LDI
ALU_A = I_Immediate;
end
'b0_0000_xxxx: begin // PUT
'b0_0000_????: begin // PUT
Waddr = A_operand;
end
'b0_0001_xxxx: begin // GET
'b0_0001_????: begin // GET
RaddrA = A_operand;
end
'b0_0010_0xxx: begin // LDW
'b0_0010_0???: begin // LDW
RaddrA = S_operand;
RegInput = mem_out;
end
'b0_0010_1xxx: begin // STW
'b0_0010_1???: begin // STW
RaddrA = S_operand;
RegWrite = 'b0;
write_mem = 'b1;
end
'b0_0011_0xxx: begin // NXT
if(S_operand == 'd8 || S_operand == 'd9 || S_operand == 'd10) ALU_OP = INC;
else if (S_operand == 'd11 || S_operand == 'd12 || S_operand == 'd13) ALU_OP = DEC;
'b0_0011_0???: begin // N?T
if(S_operand == 'd8 || S_operand == 'd9 || S_operand == 'd10) ALU_OP = DEC;
else if (S_operand == 'd11 || S_operand == 'd12 || S_operand == 'd13) ALU_OP = INC;
else ALU_OP = NOP;
RaddrA = S_operand;
Waddr = S_operand;
end
'b0_0011_1xxx: begin //CLB
'b0_0011_1???: begin //CLB
ALU_OP = CLB;
RaddrA = G_operand;
Waddr = G_operand;
end
'b0_0100_xxxx: begin // ADD
'b0_0100_????: begin // ADD
ALU_OP = ADD;
RaddrB = A_operand;
end
'b0_0101_xxxx: begin // SUB
'b0_0101_????: begin // SUB
ALU_OP = SUB;
RaddrB = A_operand;
end
'b0_0110_xxxx: begin // ORR
'b0_0110_????: begin // ORR
ALU_OP = ORR;
RaddrB = A_operand;
end
'b0_0111_xxxx: begin // AND
'b0_0111_????: begin // AND
ALU_OP = AND;
RaddrB = A_operand;
end
'b0_1000_0xxx: begin // LSH
'b0_1000_0???: begin // LSH
ALU_OP = LSH;
ALU_A = T_Immediate;
end
'b0_1000_1xxx: begin // PTY
'b0_1000_1???: begin // PTY
ALU_OP = RXOR_7;
RaddrA = G_operand;
end
'b0_1001_xxxx: begin // CHK
'b0_1001_????: begin // CHK
ALU_OP = RXOR_8;
RaddrA = A_operand;
end
'b0_1010_xxxx: begin // XOR
'b0_1010_????: begin // XOR
ALU_OP = XOR;
RaddrB = A_operand;
end
'b0_1011_xxxx: begin // DNE
'b0_1011_????: begin // DNE
Done_in = 'b1;
end
'b0_1110_0xxx: begin // JNZ
'b0_1110_0???: begin // JNZ
RegWrite = 'b0;
RaddrA = G_operand;
BranchNZ = 'b1;
end
'b0_1110_1xxx: begin // JEZ
'b0_1110_1???: begin // JEZ
RegWrite = 'b0;
RaddrA = G_operand;
BranchEZ = 'b1;
end
'b0_1111_0xxx: begin // JMP
'b0_1111_0???: begin // JMP
RegWrite = 'b0;
RaddrA = G_operand;
BranchAlways = 'b1;
end
'b0_1111_1xxx: begin // JAL
'b0_1111_1???: begin // JAL
RaddrA = G_operand;
Waddr = 'd14; // write to link register specifically
RegInput = ProgCtr_p4; // write the value pc+4
RegInput = ProgCtr_p1; // write the value pc+4
BranchAlways = 'b1;
end
endcase

View File

@ -8,18 +8,21 @@ module InstFetch #(parameter T=10, parameter W=8)( // T is PC address size, W
input logic Done, // Done flag to indicate if the PC should increment at all
input logic [W-1:0] Target, // jump target pointer
output logic [T-1:0] ProgCtr,
output logic [T-1:0] ProgCtr_p4 // value of pc+4 for use in JAL instruction itself
output logic [T-1:0] ProgCtr_p1 // value of pc+1 for use in JAL instruction itself
);
logic [T-1:0] PC;
always_ff @(posedge Clk) begin
if(Reset) PC <= 0; // if reset, set PC to 0
else if (BranchAlways) PC <= Target; // if unconditional branch, assign PC to target
else if (BranchEZ && Zero) PC <= Target; // if branch on zero and zero is true, then assign PC to target
else if (BranchNZ && !Zero) PC <= Target; // if branch on non zero and zero is false, then assign PC to parget
else if (BranchAlways) PC[W-1:0] <= Target; // if unconditional branch, assign PC to target
else if (BranchEZ && Zero) PC[W-1:0] <= Target; // if branch on zero and zero is true, then assign PC to target
else if (BranchNZ && !Zero) PC[W-1:0] <= Target; // if branch on non zero and zero is false, then assign PC to parget
else if (!Done) PC <= PC + 'b1; // if not a branch but CPU is not done, then
else PC <= PC;
end
assign ProgCtr = PC;
assign ProgCtr_p1 = ProgCtr + 1;
endmodule

View File

@ -10,7 +10,7 @@
//
// A = program counter width
// W = machine code width -- do not change for CSE141L
module InstROM #(parameter A=12, W=9) (
module InstROM #(parameter A=10, W=9) (
input [A-1:0] InstAddress,
output logic[W-1:0] InstOut);

View File

@ -2,7 +2,7 @@
// testbench for programmable message encryption (Program #1)
// CSE141L
// runs program 1 (encrypt a message)
module encrypt_tb () ;
module program1_tb () ;
// DUT interface -- four one-bit wires, three to DUT, one from
bit clk , // advances simulation step-by-step
init = 1'b1 , // init (reset) command to DUT
@ -46,7 +46,7 @@ module encrypt_tb () ;
assign LFSR_ptrn[7] = 8'h7E; // (0)111_1110
assign LFSR_ptrn[8] = 8'h7B;
always_comb begin
pt_no = 0;//$random; // or select a specific pattern ([0] and [1] are simplest to debug
pt_no = 8;//$random; // or select a specific pattern ([0] and [1] are simplest to debug
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

View File

@ -15,12 +15,13 @@ module top_level(
logic [8:0] Instruction;
logic [W-1:0] ALU_Out;
logic [W-1:0] RegOutA, RegOutB; // select from register inputs or immediate inputs
logic [T-1:0] ProgCtr_p4;
logic [T-1:0] ProgCtr_p1;
logic [W-1:0] mem_out;
op_mne ALU_OP; // control ALU operation
logic [W-1:0] ALU_A, ALU_B;
logic RegWrite, Done_in;
logic [3:0] RaddrA, RaddrB, Waddr, RegInput;
logic [3:0] RaddrA, RaddrB, Waddr;
logic [W-1:0] RegInput;
logic BranchEZ, BranchNZ, BranchAlways;
logic write_mem;
logic Zero_in, Zero_out;
@ -47,7 +48,7 @@ module top_level(
.Done(Done_out),
.Target(ALU_A),
.ProgCtr(ProgCtr),
.ProgCtr_p4(ProgCtr_p4)
.ProgCtr_p1(ProgCtr_p1)
);
RegFile regfile (
@ -67,7 +68,7 @@ module top_level(
.Done_out(Done_out)
);
DataMem datamem (
DataMem DM (
.Clk(clk),
.Reset(init),
.WriteEn(write_mem),

View File

@ -99,7 +99,8 @@ def get_immediate(operand, labels):
output = sys.argv[1]
targets = sys.argv[2:]
out = open(output, "wb")
#out = open(output, "wb")
out = open(output, "w")
print('detected targets: ' + str(targets))
for file in targets:
print('assembing: ' + file)
@ -139,4 +140,5 @@ for file in targets:
for inst in tqdm(instructions, desc='Assembly', unit=' instructions'):
opcode = op_codes[inst[0]]
operand = inst[1]
out.write((opcode| operand).to_bytes(length=2, byteorder='big'))
out.write(format(opcode | operand, 'b') + '\n')
#out.write((opcode| operand).to_bytes(length=2, byteorder='big'))

View File

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

View File

@ -24,7 +24,7 @@ init: LDI #d62
PUT r8 // init r8 decrementer with number of preamble space chars
LDI #d64
PUT r9 // init r9 decrementer to total number of possible ciphertext chars
preamble_loop: LDI #d32 // get space character decimal 32
preamble_loop: LDI #d0 // get space character decimal 0
XOR r7 // bitwise XOR the current state with plaintext space to generate ciphertext
CLB r0 // clear the leading bit of the ciphertext as in requirements
STW r12 // store ciphertext to write pointer

View File

@ -59,7 +59,7 @@ tap_init: LDI #d64
NXT r9 // decrement total encryption chars remaining
tap_loop: LDI lfsr_routine
JAL r0 // jump to lfsr routine which calculates next state in r7
LDI #d32 // load space char expected plaintext
LDI #d0 // load space char expected plaintext
XOR r7
CLB r0 // clear leading bit in the expected ciphertext
PUT r1 // store expected cipher text in r1

View File

@ -59,7 +59,7 @@ tap_init: LDI #d64
NXT r9 // decrement total encryption chars remaining
tap_loop: LDI lfsr_routine
JAL r0 // jump to lfsr routine which calculates next state in r7
LDI #d32 // load space char expected plaintext
LDI #d0 // load space char expected plaintext
XOR r7
CLB r0 // clear leading bit in the expected ciphertext
PUT r1 // store expected cipher text in r1