update cpu seq item constraints and weighting biases
This commit is contained in:
+132
-108
@@ -6,132 +6,156 @@
|
|||||||
* All cpu_seq_item transactions get converted into cpu_xbar_item transactions by
|
* All cpu_seq_item transactions get converted into cpu_xbar_item transactions by
|
||||||
* the sequencer.
|
* the sequencer.
|
||||||
*/
|
*/
|
||||||
class cpu_seq_item #(int DataSize=32, int AddrSize=14);
|
class cpu_seq_item #(
|
||||||
|
int DataSize = 32,
|
||||||
|
int AddrSize = 14
|
||||||
|
);
|
||||||
|
|
||||||
// Randomizable instruction fields
|
// Randomizable instruction fields
|
||||||
rand instr_e op;
|
rand instr_e op;
|
||||||
rand logic [4:0] rs1;
|
rand logic [ 4:0] rs1;
|
||||||
rand logic [4:0] rs2;
|
rand logic [ 4:0] rs2;
|
||||||
rand logic [4:0] rd;
|
rand logic [ 4:0] rd;
|
||||||
rand int imm;
|
rand int imm;
|
||||||
|
|
||||||
// Set by the sequencer before randomize()
|
// Set by the sequencer before randomize()
|
||||||
int idx; // Position of this instruction in the program
|
int idx; // Position of this instruction in the program
|
||||||
int prog_len; // Length used by branch constraints
|
int prog_len; // Length used by branch constraints
|
||||||
|
|
||||||
// Architectural register values when this instruction would execute.
|
// Architectural register values when this instruction would execute.
|
||||||
// The sequencer supplies this context before randomize(). rs1 is still
|
// The sequencer supplies this context before randomize(). rs1 is still
|
||||||
// randomized; this array tells a constraint the value of the selected
|
// randomized; this array tells a constraint the value of the selected
|
||||||
// base register without reserving or forcing an address register.
|
// base register without reserving or forcing an address register.
|
||||||
logic [REG_COUNT-1:0][31:0] reg_snapshot;
|
logic [REG_COUNT-1:0][31:0] reg_snapshot;
|
||||||
|
|
||||||
// Keep generated instructions within the supported instruction set
|
// Keep generated instructions within the supported instruction set
|
||||||
constraint c_opcode {
|
constraint c_opcode {op inside {OP_ADD, OP_ADDI, OP_SUB, OP_SLL, OP_SRL, OP_LW, OP_SW, OP_BEQ};}
|
||||||
op inside {OP_ADD, OP_ADDI, OP_SUB, OP_SLL, OP_SRL, OP_LW, OP_SW, OP_BEQ};
|
|
||||||
|
// Weighted distributions for registers to increase likelihood of register x0 in testing
|
||||||
|
constraint c_reg_bias {
|
||||||
|
rd dist {
|
||||||
|
0 :/ 5,
|
||||||
|
[1 : 7] :/ 70,
|
||||||
|
[8 : 31] :/ 25
|
||||||
|
};
|
||||||
|
rs1 dist {
|
||||||
|
0 :/ 10,
|
||||||
|
[1 : 7] :/ 70,
|
||||||
|
[8 : 31] :/ 20
|
||||||
|
};
|
||||||
|
rs2 dist {
|
||||||
|
0 :/ 10,
|
||||||
|
[1 : 7] :/ 70,
|
||||||
|
[8 : 31] :/ 20
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
constraint c_imm_range {
|
||||||
|
(op inside {OP_ADDI, OP_SW, OP_LW}) -> {
|
||||||
|
imm >= -2048;
|
||||||
|
imm <= 2047;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Weighted distributions for registers to increase likelihood of register x0 in testing
|
(op == OP_BEQ) -> {
|
||||||
constraint c_reg_bias {
|
imm >= -4096;
|
||||||
rd dist { 0 :/ 5, [1:7] :/ 70, [8:31] :/ 25 };
|
imm <= 4094;
|
||||||
rs1 dist { 0 :/ 10, [1:7] :/ 70, [8:31] :/ 20 };
|
|
||||||
rs2 dist { 0 :/ 10, [1:7] :/ 70, [8:31] :/ 20 };
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Complete the immediate constraint
|
// Keep each load/store effective byte address word-aligned and inside
|
||||||
constraint c_imm_range {
|
// data SRAM. reg_snapshot[rs1] gives the runtime value of whatever
|
||||||
imm >= -2048;
|
// register rs1 happens to randomize to, so the constraint reasons about
|
||||||
|
// reg_snapshot[rs1] + imm (the real effective address) instead of imm
|
||||||
|
// alone. The addition and comparisons use the same unsigned, wrapping
|
||||||
|
// 32-bit arithmetic as the DUT's own alu_result, so this matches what
|
||||||
|
// dsram_addr_o actually decodes.
|
||||||
|
constraint c_mem_align {
|
||||||
|
(op inside {OP_LW, OP_SW}) -> {
|
||||||
|
imm >= -2048;
|
||||||
|
imm <= 2047;
|
||||||
|
(reg_snapshot[rs1] + imm) % 4 == 0;
|
||||||
|
(reg_snapshot[rs1] + imm) <= (DSRAM_WORDS * 4 - 4);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Keep each load/store effective byte address word-aligned and inside
|
// Immediate values are constrained to 0 during these register-only instructions
|
||||||
// data SRAM. reg_snapshot[rs1] gives the runtime value of whatever
|
constraint c_imm_unused {
|
||||||
// register rs1 happens to randomize to, so the constraint reasons about
|
(op inside {OP_ADD, OP_SUB, OP_SLL, OP_SRL}) ->
|
||||||
// reg_snapshot[rs1] + imm (the real effective address) instead of imm
|
imm == 0;
|
||||||
// alone. The addition and comparisons use the same unsigned, wrapping
|
}
|
||||||
// 32-bit arithmetic as the DUT's own alu_result, so this matches what
|
|
||||||
// dsram_addr_o actually decodes.
|
constraint c_branch_target {
|
||||||
constraint c_mem_align {
|
(op == OP_BEQ) -> {
|
||||||
(op inside {OP_LW, OP_SW}) -> {
|
imm % 4 == 0;
|
||||||
imm >= -2048;
|
imm >= 4;
|
||||||
imm <= 2047;
|
imm <= (prog_len - idx - 1) * 4;
|
||||||
(reg_snapshot[rs1] + imm) % 4 == 0;
|
|
||||||
(reg_snapshot[rs1] + imm) <= (DSRAM_WORDS * 4 - 4);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Immediate values are constrained to 0 during these register-only instructions
|
// Prevent a branch in the final generated slot
|
||||||
constraint c_imm_unused {
|
constraint c_no_branch_at_end {(prog_len - idx < 2) -> op != OP_BEQ;}
|
||||||
(op inside {OP_ADD, OP_SUB, OP_SLL, OP_SRL}) -> imm == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Complete the branch constraint to avoid loops
|
constraint c_branch_taken_bias {
|
||||||
constraint c_branch_target {
|
(op == OP_BEQ) ->
|
||||||
(op == OP_BEQ) -> (imm % 4 == 0) && (imm >= 4);
|
rs2 dist {
|
||||||
}
|
[rs1 : rs1] :/ 40,
|
||||||
|
[ 0 : 31] :/ 60
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Prevent a branch in the final generated slot
|
// Constructor
|
||||||
constraint c_no_branch_at_end {
|
function new();
|
||||||
(prog_len - idx < 2) -> op != OP_BEQ;
|
idx = 0;
|
||||||
}
|
prog_len = 1;
|
||||||
|
reg_snapshot = '0;
|
||||||
|
endfunction : new
|
||||||
|
|
||||||
// TODO: Randomly-generated registers rarely compare equal. Bias the operands
|
// Encode the instruction fields into an instruction-memory word
|
||||||
// so both branch outcomes occur often enough to close coverage.
|
function logic [31:0] encode();
|
||||||
constraint c_branch_taken_bias {
|
logic [31:0] word;
|
||||||
1;
|
logic [11:0] i_imm;
|
||||||
}
|
logic [11:0] s_imm;
|
||||||
|
logic [12:0] b_imm;
|
||||||
|
|
||||||
// Constructor
|
i_imm = imm[11:0];
|
||||||
function new();
|
s_imm = imm[11:0];
|
||||||
idx = 0;
|
b_imm = imm[12:0];
|
||||||
prog_len = 1;
|
word = '0;
|
||||||
reg_snapshot = '0;
|
|
||||||
endfunction: new
|
|
||||||
|
|
||||||
// Encode the instruction fields into an instruction-memory word
|
case (op)
|
||||||
function logic [31:0] encode();
|
OP_ADD: word = {7'b0000000, rs2, rs1, 3'b000, rd, 7'b0110011};
|
||||||
logic [31:0] word;
|
OP_SUB: word = {7'b0100000, rs2, rs1, 3'b000, rd, 7'b0110011};
|
||||||
logic [11:0] i_imm;
|
OP_SLL: word = {7'b0000000, rs2, rs1, 3'b001, rd, 7'b0110011};
|
||||||
logic [11:0] s_imm;
|
OP_SRL: word = {7'b0000000, rs2, rs1, 3'b101, rd, 7'b0110011};
|
||||||
logic [12:0] b_imm;
|
OP_ADDI: word = {i_imm, rs1, 3'b000, rd, 7'b0010011};
|
||||||
|
OP_LW: word = {i_imm, rs1, 3'b010, rd, 7'b0000011};
|
||||||
|
OP_SW: word = {s_imm[11:5], rs2, rs1, 3'b010, s_imm[4:0], 7'b0100011};
|
||||||
|
OP_BEQ: word = {b_imm[12], b_imm[10:5], rs2, rs1, 3'b000, b_imm[4:1], b_imm[11], 7'b1100011};
|
||||||
|
OP_EBREAK: word = EBREAK_WORD;
|
||||||
|
default: word = '0; // Decodes to a NOP
|
||||||
|
endcase
|
||||||
|
|
||||||
i_imm = imm[11:0];
|
return word;
|
||||||
s_imm = imm[11:0];
|
endfunction : encode
|
||||||
b_imm = imm[12:0];
|
|
||||||
word = '0;
|
|
||||||
|
|
||||||
case (op)
|
// Format the instruction for logs and scoreboard failures
|
||||||
OP_ADD: word = {7'b0000000, rs2, rs1, 3'b000, rd, 7'b0110011};
|
function string asm();
|
||||||
OP_SUB: word = {7'b0100000, rs2, rs1, 3'b000, rd, 7'b0110011};
|
case (op)
|
||||||
OP_SLL: word = {7'b0000000, rs2, rs1, 3'b001, rd, 7'b0110011};
|
OP_ADD: return $sformatf("add x%0d, x%0d, x%0d", rd, rs1, rs2);
|
||||||
OP_SRL: word = {7'b0000000, rs2, rs1, 3'b101, rd, 7'b0110011};
|
OP_SUB: return $sformatf("sub x%0d, x%0d, x%0d", rd, rs1, rs2);
|
||||||
OP_ADDI: word = {i_imm, rs1, 3'b000, rd, 7'b0010011};
|
OP_SLL: return $sformatf("sll x%0d, x%0d, x%0d", rd, rs1, rs2);
|
||||||
OP_LW: word = {i_imm, rs1, 3'b010, rd, 7'b0000011};
|
OP_SRL: return $sformatf("srl x%0d, x%0d, x%0d", rd, rs1, rs2);
|
||||||
OP_SW: word = {s_imm[11:5], rs2, rs1, 3'b010, s_imm[4:0], 7'b0100011};
|
OP_ADDI: return $sformatf("addi x%0d, x%0d, %0d", rd, rs1, imm);
|
||||||
OP_BEQ: word = {b_imm[12], b_imm[10:5], rs2, rs1, 3'b000, b_imm[4:1], b_imm[11], 7'b1100011};
|
OP_LW: return $sformatf("lw x%0d, %0d(x%0d)", rd, imm, rs1);
|
||||||
OP_EBREAK: word = EBREAK_WORD;
|
OP_SW: return $sformatf("sw x%0d, %0d(x%0d)", rs2, imm, rs1);
|
||||||
default: word = '0; // Decodes to a NOP
|
OP_BEQ: return $sformatf("beq x%0d, x%0d, %0d", rs1, rs2, imm);
|
||||||
endcase
|
OP_EBREAK: return "ebreak";
|
||||||
|
default: return "nop";
|
||||||
|
endcase
|
||||||
|
endfunction : asm
|
||||||
|
|
||||||
return word;
|
function void display();
|
||||||
endfunction: encode
|
$display($stime, " Seq: [%0d] 0x%08x %s\n", idx, encode(), asm());
|
||||||
|
endfunction : display
|
||||||
|
|
||||||
// Format the instruction for logs and scoreboard failures
|
endclass : cpu_seq_item
|
||||||
function string asm();
|
|
||||||
case (op)
|
|
||||||
OP_ADD: return $sformatf("add x%0d, x%0d, x%0d", rd, rs1, rs2);
|
|
||||||
OP_SUB: return $sformatf("sub x%0d, x%0d, x%0d", rd, rs1, rs2);
|
|
||||||
OP_SLL: return $sformatf("sll x%0d, x%0d, x%0d", rd, rs1, rs2);
|
|
||||||
OP_SRL: return $sformatf("srl x%0d, x%0d, x%0d", rd, rs1, rs2);
|
|
||||||
OP_ADDI: return $sformatf("addi x%0d, x%0d, %0d", rd, rs1, imm);
|
|
||||||
OP_LW: return $sformatf("lw x%0d, %0d(x%0d)", rd, imm, rs1);
|
|
||||||
OP_SW: return $sformatf("sw x%0d, %0d(x%0d)", rs2, imm, rs1);
|
|
||||||
OP_BEQ: return $sformatf("beq x%0d, x%0d, %0d", rs1, rs2, imm);
|
|
||||||
OP_EBREAK: return "ebreak";
|
|
||||||
default: return "nop";
|
|
||||||
endcase
|
|
||||||
endfunction: asm
|
|
||||||
|
|
||||||
function void display();
|
|
||||||
$display($stime, " Seq: [%0d] 0x%08x %s\n", idx, encode(), asm());
|
|
||||||
endfunction: display
|
|
||||||
|
|
||||||
endclass: cpu_seq_item
|
|
||||||
|
|||||||
Reference in New Issue
Block a user