| 1 |
2 |
dgisselq |
///////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//
|
| 3 |
|
|
// Filename: cpuops.v
|
| 4 |
|
|
//
|
| 5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
| 6 |
|
|
//
|
| 7 |
|
|
// Purpose:
|
| 8 |
|
|
//
|
| 9 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
| 10 |
|
|
// Gisselquist Tecnology, LLC
|
| 11 |
|
|
//
|
| 12 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 13 |
|
|
//
|
| 14 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
| 15 |
|
|
//
|
| 16 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
| 17 |
|
|
// modify it under the terms of the GNU General Public License as published
|
| 18 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 19 |
|
|
// your option) any later version.
|
| 20 |
|
|
//
|
| 21 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 22 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 23 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 24 |
|
|
// for more details.
|
| 25 |
|
|
//
|
| 26 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 27 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 28 |
|
|
//
|
| 29 |
|
|
//
|
| 30 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 31 |
|
|
//
|
| 32 |
56 |
dgisselq |
module cpuops(i_clk, i_rst, i_ce, i_valid, i_op, i_a, i_b, o_c, o_f, o_valid,
|
| 33 |
|
|
o_illegal);
|
| 34 |
|
|
parameter IMPLEMENT_MPY = 1;
|
| 35 |
2 |
dgisselq |
input i_clk, i_rst, i_ce;
|
| 36 |
|
|
input [3:0] i_op;
|
| 37 |
|
|
input [31:0] i_a, i_b;
|
| 38 |
|
|
input i_valid;
|
| 39 |
|
|
output reg [31:0] o_c;
|
| 40 |
|
|
output wire [3:0] o_f;
|
| 41 |
|
|
output reg o_valid;
|
| 42 |
56 |
dgisselq |
output wire o_illegal;
|
| 43 |
2 |
dgisselq |
|
| 44 |
62 |
dgisselq |
// Rotate-left pre-logic
|
| 45 |
2 |
dgisselq |
wire [63:0] w_rol_tmp;
|
| 46 |
|
|
assign w_rol_tmp = { i_a, i_a } << i_b[4:0];
|
| 47 |
|
|
wire [31:0] w_rol_result;
|
| 48 |
|
|
assign w_rol_result = w_rol_tmp[63:32]; // Won't set flags
|
| 49 |
62 |
dgisselq |
|
| 50 |
|
|
// Shift register pre-logic
|
| 51 |
56 |
dgisselq |
wire [32:0] w_lsr_result, w_asr_result;
|
| 52 |
|
|
assign w_asr_result = (|i_b[31:5])? {(33){i_a[31]}}
|
| 53 |
|
|
: ( {i_a, 1'b0 } >>> (i_b[4:0]) );// ASR
|
| 54 |
|
|
assign w_lsr_result = (|i_b[31:5])? 33'h00
|
| 55 |
|
|
: ( { i_a, 1'b0 } >> (i_b[4:0]) );// LSR
|
| 56 |
2 |
dgisselq |
|
| 57 |
25 |
dgisselq |
|
| 58 |
2 |
dgisselq |
wire z, n, v;
|
| 59 |
|
|
reg c, pre_sign, set_ovfl;
|
| 60 |
|
|
always @(posedge i_clk)
|
| 61 |
|
|
if (i_ce)
|
| 62 |
|
|
set_ovfl =((((i_op==4'h0)||(i_op==4'h8)) // SUB&CMP
|
| 63 |
|
|
&&(i_a[31] != i_b[31]))
|
| 64 |
|
|
||((i_op==4'ha)&&(i_a[31] == i_b[31])) // ADD
|
| 65 |
|
|
||(i_op == 4'hd) // LSL
|
| 66 |
|
|
||(i_op == 4'hf)); // LSR
|
| 67 |
56 |
dgisselq |
|
| 68 |
62 |
dgisselq |
|
| 69 |
|
|
// A 4-way multiplexer can be done in one 6-LUT.
|
| 70 |
|
|
// A 16-way multiplexer can therefore be done in 4x 6-LUT's with
|
| 71 |
|
|
// the Xilinx multiplexer fabric that follows.
|
| 72 |
|
|
// Given that we wish to apply this multiplexer approach to 33-bits,
|
| 73 |
|
|
// this will cost a minimum of 132 6-LUTs.
|
| 74 |
56 |
dgisselq |
generate
|
| 75 |
|
|
if (IMPLEMENT_MPY == 0)
|
| 76 |
|
|
begin
|
| 77 |
|
|
always @(posedge i_clk)
|
| 78 |
2 |
dgisselq |
if (i_ce)
|
| 79 |
|
|
begin
|
| 80 |
|
|
pre_sign <= (i_a[31]);
|
| 81 |
|
|
c <= 1'b0;
|
| 82 |
3 |
dgisselq |
casez(i_op)
|
| 83 |
62 |
dgisselq |
4'b?000:{c,o_c } <= {1'b0,i_a} - {1'b0,i_b};// CMP/SUB
|
| 84 |
3 |
dgisselq |
4'b?001: o_c <= i_a & i_b; // BTST/And
|
| 85 |
62 |
dgisselq |
// 4'h3: There's a hole here for the unimplemented MPYU,
|
| 86 |
|
|
// 4'h4: and here for the unimplemented MPYS
|
| 87 |
56 |
dgisselq |
4'h5: o_c <= w_rol_result; // ROL
|
| 88 |
|
|
4'h6: o_c <= { i_a[31:16], i_b[15:0] }; // LODILO
|
| 89 |
62 |
dgisselq |
4'h7: o_c <= { i_b[15: 0], i_a[15:0] }; // LODIHI
|
| 90 |
56 |
dgisselq |
4'ha: { c, o_c } <= i_a + i_b; // Add
|
| 91 |
|
|
4'hb: o_c <= i_a | i_b; // Or
|
| 92 |
|
|
4'hc: o_c <= i_a ^ i_b; // Xor
|
| 93 |
|
|
4'hd: { c, o_c } <= (|i_b[31:5])? 33'h00 : {1'b0, i_a } << i_b[4:0]; // LSL
|
| 94 |
62 |
dgisselq |
4'he: { o_c, c } <= w_asr_result[32:0]; // ASR
|
| 95 |
|
|
4'hf: { o_c, c } <= w_lsr_result[32:0]; // LSR
|
| 96 |
56 |
dgisselq |
default: o_c <= i_b; // MOV, LDI
|
| 97 |
|
|
endcase
|
| 98 |
|
|
end
|
| 99 |
|
|
end else begin
|
| 100 |
62 |
dgisselq |
//
|
| 101 |
|
|
// Multiply pre-logic
|
| 102 |
|
|
//
|
| 103 |
56 |
dgisselq |
wire signed [16:0] w_mpy_a_input, w_mpy_b_input;
|
| 104 |
|
|
wire signed [33:0] w_mpy_result;
|
| 105 |
|
|
assign w_mpy_a_input = { ((i_a[15])&&(i_op[2])), i_a[15:0] };
|
| 106 |
|
|
assign w_mpy_b_input = { ((i_b[15])&&(i_op[2])), i_b[15:0] };
|
| 107 |
|
|
assign w_mpy_result = w_mpy_a_input * w_mpy_b_input;
|
| 108 |
|
|
|
| 109 |
62 |
dgisselq |
|
| 110 |
|
|
//
|
| 111 |
|
|
// The master ALU case statement
|
| 112 |
|
|
//
|
| 113 |
56 |
dgisselq |
always @(posedge i_clk)
|
| 114 |
|
|
if (i_ce)
|
| 115 |
|
|
begin
|
| 116 |
|
|
pre_sign <= (i_a[31]);
|
| 117 |
|
|
c <= 1'b0;
|
| 118 |
|
|
casez(i_op)
|
| 119 |
62 |
dgisselq |
4'b?000:{c,o_c } <= {1'b0,i_a} - {1'b0,i_b};// CMP/SUB
|
| 120 |
56 |
dgisselq |
4'b?001: o_c <= i_a & i_b; // BTST/And
|
| 121 |
62 |
dgisselq |
4'h3: { c, o_c } <= {1'b0,w_mpy_result[31:0]}; // MPYU
|
| 122 |
|
|
4'h4: { c, o_c } <= {1'b0,w_mpy_result[31:0]}; // MPYS
|
| 123 |
2 |
dgisselq |
4'h5: o_c <= w_rol_result; // ROL
|
| 124 |
|
|
4'h6: o_c <= { i_a[31:16], i_b[15:0] }; // LODILO
|
| 125 |
62 |
dgisselq |
4'h7: o_c <= { i_b[15: 0], i_a[15:0] }; // LODIHI
|
| 126 |
2 |
dgisselq |
4'ha: { c, o_c } <= i_a + i_b; // Add
|
| 127 |
|
|
4'hb: o_c <= i_a | i_b; // Or
|
| 128 |
|
|
4'hc: o_c <= i_a ^ i_b; // Xor
|
| 129 |
12 |
dgisselq |
4'hd: { c, o_c } <= (|i_b[31:5])? 33'h00 : {1'b0, i_a } << i_b[4:0]; // LSL
|
| 130 |
62 |
dgisselq |
4'he: { o_c, c } <= w_asr_result[32:0]; // ASR
|
| 131 |
|
|
4'hf: { o_c, c } <= w_lsr_result[32:0]; // LSR
|
| 132 |
2 |
dgisselq |
default: o_c <= i_b; // MOV, LDI
|
| 133 |
|
|
endcase
|
| 134 |
|
|
end
|
| 135 |
56 |
dgisselq |
end endgenerate
|
| 136 |
2 |
dgisselq |
|
| 137 |
56 |
dgisselq |
generate
|
| 138 |
|
|
if (IMPLEMENT_MPY == 0)
|
| 139 |
|
|
begin
|
| 140 |
|
|
reg r_illegal;
|
| 141 |
|
|
always @(posedge i_clk)
|
| 142 |
62 |
dgisselq |
r_illegal <= (i_ce)&&((i_op == 4'h3)||(i_op == 4'h4));
|
| 143 |
56 |
dgisselq |
assign o_illegal = r_illegal;
|
| 144 |
|
|
end else
|
| 145 |
|
|
assign o_illegal = 1'b0;
|
| 146 |
|
|
endgenerate
|
| 147 |
|
|
|
| 148 |
2 |
dgisselq |
assign z = (o_c == 32'h0000);
|
| 149 |
|
|
assign n = (o_c[31]);
|
| 150 |
|
|
assign v = (set_ovfl)&&(pre_sign != o_c[31]);
|
| 151 |
|
|
|
| 152 |
|
|
assign o_f = { v, n, c, z };
|
| 153 |
|
|
|
| 154 |
|
|
initial o_valid = 1'b0;
|
| 155 |
|
|
always @(posedge i_clk)
|
| 156 |
|
|
if (i_rst)
|
| 157 |
|
|
o_valid <= 1'b0;
|
| 158 |
56 |
dgisselq |
else
|
| 159 |
|
|
o_valid <= (i_ce)&&(i_valid);
|
| 160 |
2 |
dgisselq |
endmodule
|