| 1 |
2 |
dgisselq |
///////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//
|
| 3 |
|
|
// Filename: cpuops.v
|
| 4 |
|
|
//
|
| 5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
| 6 |
|
|
//
|
| 7 |
69 |
dgisselq |
// Purpose: This supports the instruction set reordering of operations
|
| 8 |
|
|
// created by the second generation instruction set, as well as
|
| 9 |
|
|
// the new operations of POPC (population count) and BREV (bit reversal).
|
| 10 |
2 |
dgisselq |
//
|
| 11 |
69 |
dgisselq |
//
|
| 12 |
2 |
dgisselq |
// Creator: Dan Gisselquist, Ph.D.
|
| 13 |
69 |
dgisselq |
// Gisselquist Technology, LLC
|
| 14 |
2 |
dgisselq |
//
|
| 15 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 16 |
|
|
//
|
| 17 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
| 18 |
|
|
//
|
| 19 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
| 20 |
|
|
// modify it under the terms of the GNU General Public License as published
|
| 21 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 22 |
|
|
// your option) any later version.
|
| 23 |
|
|
//
|
| 24 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 25 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 26 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 27 |
|
|
// for more details.
|
| 28 |
|
|
//
|
| 29 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 30 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 31 |
|
|
//
|
| 32 |
|
|
//
|
| 33 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 34 |
|
|
//
|
| 35 |
69 |
dgisselq |
module cpuops(i_clk,i_rst, i_ce, i_valid, i_op, i_a, i_b, o_c, o_f, o_valid,
|
| 36 |
71 |
dgisselq |
o_illegal, o_busy);
|
| 37 |
56 |
dgisselq |
parameter IMPLEMENT_MPY = 1;
|
| 38 |
2 |
dgisselq |
input i_clk, i_rst, i_ce;
|
| 39 |
|
|
input [3:0] i_op;
|
| 40 |
|
|
input [31:0] i_a, i_b;
|
| 41 |
|
|
input i_valid;
|
| 42 |
|
|
output reg [31:0] o_c;
|
| 43 |
|
|
output wire [3:0] o_f;
|
| 44 |
|
|
output reg o_valid;
|
| 45 |
56 |
dgisselq |
output wire o_illegal;
|
| 46 |
71 |
dgisselq |
output wire o_busy;
|
| 47 |
2 |
dgisselq |
|
| 48 |
62 |
dgisselq |
// Rotate-left pre-logic
|
| 49 |
2 |
dgisselq |
wire [63:0] w_rol_tmp;
|
| 50 |
|
|
assign w_rol_tmp = { i_a, i_a } << i_b[4:0];
|
| 51 |
|
|
wire [31:0] w_rol_result;
|
| 52 |
|
|
assign w_rol_result = w_rol_tmp[63:32]; // Won't set flags
|
| 53 |
62 |
dgisselq |
|
| 54 |
|
|
// Shift register pre-logic
|
| 55 |
56 |
dgisselq |
wire [32:0] w_lsr_result, w_asr_result;
|
| 56 |
|
|
assign w_asr_result = (|i_b[31:5])? {(33){i_a[31]}}
|
| 57 |
|
|
: ( {i_a, 1'b0 } >>> (i_b[4:0]) );// ASR
|
| 58 |
|
|
assign w_lsr_result = (|i_b[31:5])? 33'h00
|
| 59 |
|
|
: ( { i_a, 1'b0 } >> (i_b[4:0]) );// LSR
|
| 60 |
2 |
dgisselq |
|
| 61 |
69 |
dgisselq |
// Bit reversal pre-logic
|
| 62 |
|
|
wire [31:0] w_brev_result;
|
| 63 |
|
|
genvar k;
|
| 64 |
|
|
generate
|
| 65 |
|
|
for(k=0; k<32; k=k+1)
|
| 66 |
|
|
assign w_brev_result[k] = i_b[31-k];
|
| 67 |
|
|
endgenerate
|
| 68 |
25 |
dgisselq |
|
| 69 |
69 |
dgisselq |
// Popcount pre-logic
|
| 70 |
|
|
wire [31:0] w_popc_result;
|
| 71 |
|
|
assign w_popc_result[5:0]=
|
| 72 |
|
|
({5'h0,i_b[ 0]}+{5'h0,i_b[ 1]}+{5'h0,i_b[ 2]}+{5'h0,i_b[ 3]})
|
| 73 |
|
|
+({5'h0,i_b[ 4]}+{5'h0,i_b[ 5]}+{5'h0,i_b[ 6]}+{5'h0,i_b[ 7]})
|
| 74 |
|
|
+({5'h0,i_b[ 8]}+{5'h0,i_b[ 9]}+{5'h0,i_b[10]}+{5'h0,i_b[11]})
|
| 75 |
|
|
+({5'h0,i_b[12]}+{5'h0,i_b[13]}+{5'h0,i_b[14]}+{5'h0,i_b[15]})
|
| 76 |
|
|
+({5'h0,i_b[16]}+{5'h0,i_b[17]}+{5'h0,i_b[18]}+{5'h0,i_b[19]})
|
| 77 |
|
|
+({5'h0,i_b[20]}+{5'h0,i_b[21]}+{5'h0,i_b[22]}+{5'h0,i_b[23]})
|
| 78 |
|
|
+({5'h0,i_b[24]}+{5'h0,i_b[25]}+{5'h0,i_b[26]}+{5'h0,i_b[27]})
|
| 79 |
|
|
+({5'h0,i_b[28]}+{5'h0,i_b[29]}+{5'h0,i_b[30]}+{5'h0,i_b[31]});
|
| 80 |
|
|
assign w_popc_result[31:6] = 26'h00;
|
| 81 |
|
|
|
| 82 |
|
|
// Prelogic for our flags registers
|
| 83 |
2 |
dgisselq |
wire z, n, v;
|
| 84 |
|
|
reg c, pre_sign, set_ovfl;
|
| 85 |
|
|
always @(posedge i_clk)
|
| 86 |
69 |
dgisselq |
if (i_ce) // 1 LUT
|
| 87 |
|
|
set_ovfl =(((i_op==4'h0)&&(i_a[31] != i_b[31]))//SUB&CMP
|
| 88 |
|
|
||((i_op==4'h2)&&(i_a[31] == i_b[31])) // ADD
|
| 89 |
|
|
||(i_op == 4'h6) // LSL
|
| 90 |
|
|
||(i_op == 4'h5)); // LSR
|
| 91 |
56 |
dgisselq |
|
| 92 |
62 |
dgisselq |
|
| 93 |
|
|
// A 4-way multiplexer can be done in one 6-LUT.
|
| 94 |
|
|
// A 16-way multiplexer can therefore be done in 4x 6-LUT's with
|
| 95 |
|
|
// the Xilinx multiplexer fabric that follows.
|
| 96 |
|
|
// Given that we wish to apply this multiplexer approach to 33-bits,
|
| 97 |
|
|
// this will cost a minimum of 132 6-LUTs.
|
| 98 |
56 |
dgisselq |
generate
|
| 99 |
|
|
if (IMPLEMENT_MPY == 0)
|
| 100 |
|
|
begin
|
| 101 |
|
|
always @(posedge i_clk)
|
| 102 |
2 |
dgisselq |
if (i_ce)
|
| 103 |
|
|
begin
|
| 104 |
|
|
pre_sign <= (i_a[31]);
|
| 105 |
|
|
c <= 1'b0;
|
| 106 |
3 |
dgisselq |
casez(i_op)
|
| 107 |
69 |
dgisselq |
4'b0000:{c,o_c } <= {1'b0,i_a}-{1'b0,i_b};// CMP/SUB
|
| 108 |
|
|
4'b0001: o_c <= i_a & i_b; // BTST/And
|
| 109 |
|
|
4'b0010:{c,o_c } <= i_a + i_b; // Add
|
| 110 |
|
|
4'b0011: o_c <= i_a | i_b; // Or
|
| 111 |
|
|
4'b0100: o_c <= i_a ^ i_b; // Xor
|
| 112 |
|
|
4'b0101:{o_c,c } <= w_lsr_result[32:0]; // LSR
|
| 113 |
|
|
4'b0110:{c,o_c } <= (|i_b[31:5])? 33'h00 : {1'b0, i_a } << i_b[4:0]; // LSL
|
| 114 |
|
|
4'b0111:{o_c,c } <= w_asr_result[32:0]; // ASR
|
| 115 |
|
|
4'b1000: o_c <= { i_b[15: 0], i_a[15:0] }; // LODIHI
|
| 116 |
|
|
4'b1001: o_c <= { i_a[31:16], i_b[15:0] }; // LODILO
|
| 117 |
|
|
// 4'h1010: The unimplemented MPYU,
|
| 118 |
|
|
// 4'h1011: and here for the unimplemented MPYS
|
| 119 |
|
|
4'b1100: o_c <= w_brev_result; // BREV
|
| 120 |
|
|
4'b1101: o_c <= w_popc_result; // POPC
|
| 121 |
|
|
4'b1110: o_c <= w_rol_result; // ROL
|
| 122 |
|
|
default: o_c <= i_b; // MOV, LDI
|
| 123 |
56 |
dgisselq |
endcase
|
| 124 |
|
|
end
|
| 125 |
71 |
dgisselq |
|
| 126 |
|
|
assign o_busy = 1'b0;
|
| 127 |
|
|
|
| 128 |
|
|
reg r_illegal;
|
| 129 |
|
|
always @(posedge i_clk)
|
| 130 |
|
|
r_illegal <= (i_ce)&&((i_op == 4'h3)||(i_op == 4'h4));
|
| 131 |
|
|
assign o_illegal = r_illegal;
|
| 132 |
56 |
dgisselq |
end else begin
|
| 133 |
62 |
dgisselq |
//
|
| 134 |
|
|
// Multiply pre-logic
|
| 135 |
|
|
//
|
| 136 |
71 |
dgisselq |
wire [16:0] w_mpy_a_input, w_mpy_b_input;
|
| 137 |
|
|
wire [33:0] w_mpy_result;
|
| 138 |
|
|
reg [31:0] r_mpy_result;
|
| 139 |
|
|
assign w_mpy_a_input ={ ((i_a[15])&(i_op[0])), i_a[15:0] };
|
| 140 |
|
|
assign w_mpy_b_input ={ ((i_b[15])&(i_op[0])), i_b[15:0] };
|
| 141 |
|
|
assign w_mpy_result = w_mpy_a_input * w_mpy_b_input;
|
| 142 |
|
|
always @(posedge i_clk)
|
| 143 |
|
|
if (i_ce)
|
| 144 |
|
|
r_mpy_result = w_mpy_result[31:0];
|
| 145 |
56 |
dgisselq |
|
| 146 |
62 |
dgisselq |
//
|
| 147 |
|
|
// The master ALU case statement
|
| 148 |
|
|
//
|
| 149 |
56 |
dgisselq |
always @(posedge i_clk)
|
| 150 |
|
|
if (i_ce)
|
| 151 |
|
|
begin
|
| 152 |
|
|
pre_sign <= (i_a[31]);
|
| 153 |
|
|
c <= 1'b0;
|
| 154 |
|
|
casez(i_op)
|
| 155 |
69 |
dgisselq |
4'b0000:{c,o_c } <= {1'b0,i_a}-{1'b0,i_b};// CMP/SUB
|
| 156 |
|
|
4'b0001: o_c <= i_a & i_b; // BTST/And
|
| 157 |
|
|
4'b0010:{c,o_c } <= i_a + i_b; // Add
|
| 158 |
|
|
4'b0011: o_c <= i_a | i_b; // Or
|
| 159 |
|
|
4'b0100: o_c <= i_a ^ i_b; // Xor
|
| 160 |
|
|
4'b0101:{o_c,c } <= w_lsr_result[32:0]; // LSR
|
| 161 |
|
|
4'b0110:{c,o_c } <= (|i_b[31:5])? 33'h00 : {1'b0, i_a } << i_b[4:0]; // LSL
|
| 162 |
|
|
4'b0111:{o_c,c } <= w_asr_result[32:0]; // ASR
|
| 163 |
|
|
4'b1000: o_c <= { i_b[15: 0], i_a[15:0] }; // LODIHI
|
| 164 |
|
|
4'b1001: o_c <= { i_a[31:16], i_b[15:0] }; // LODILO
|
| 165 |
71 |
dgisselq |
4'b1010: o_c <= r_mpy_result; // MPYU
|
| 166 |
|
|
4'b1011: o_c <= r_mpy_result; // MPYS
|
| 167 |
69 |
dgisselq |
4'b1100: o_c <= w_brev_result; // BREV
|
| 168 |
|
|
4'b1101: o_c <= w_popc_result; // POPC
|
| 169 |
|
|
4'b1110: o_c <= w_rol_result; // ROL
|
| 170 |
|
|
default: o_c <= i_b; // MOV, LDI
|
| 171 |
2 |
dgisselq |
endcase
|
| 172 |
71 |
dgisselq |
end else if (r_busy)
|
| 173 |
|
|
o_c <= r_mpy_result;
|
| 174 |
2 |
dgisselq |
|
| 175 |
71 |
dgisselq |
reg r_busy;
|
| 176 |
|
|
initial r_busy = 1'b0;
|
| 177 |
56 |
dgisselq |
always @(posedge i_clk)
|
| 178 |
71 |
dgisselq |
r_busy <= (~i_rst)&&(i_ce)&&(i_valid)
|
| 179 |
|
|
&&(i_op[3:1] == 3'h5);
|
| 180 |
|
|
|
| 181 |
|
|
assign o_busy = r_busy;
|
| 182 |
|
|
|
| 183 |
56 |
dgisselq |
assign o_illegal = 1'b0;
|
| 184 |
71 |
dgisselq |
end endgenerate
|
| 185 |
56 |
dgisselq |
|
| 186 |
2 |
dgisselq |
assign z = (o_c == 32'h0000);
|
| 187 |
|
|
assign n = (o_c[31]);
|
| 188 |
|
|
assign v = (set_ovfl)&&(pre_sign != o_c[31]);
|
| 189 |
|
|
|
| 190 |
|
|
assign o_f = { v, n, c, z };
|
| 191 |
|
|
|
| 192 |
|
|
initial o_valid = 1'b0;
|
| 193 |
|
|
always @(posedge i_clk)
|
| 194 |
|
|
if (i_rst)
|
| 195 |
|
|
o_valid <= 1'b0;
|
| 196 |
56 |
dgisselq |
else
|
| 197 |
71 |
dgisselq |
o_valid <= (i_ce)&&(i_valid)&&(i_op[3:1] != 3'h5)
|
| 198 |
|
|
||(o_busy);
|
| 199 |
2 |
dgisselq |
endmodule
|