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 |
|
|
module cpuops(i_clk, i_rst, i_ce, i_valid, i_op, i_a, i_b, o_c, o_f, o_valid);
|
33 |
|
|
input i_clk, i_rst, i_ce;
|
34 |
|
|
input [3:0] i_op;
|
35 |
|
|
input [31:0] i_a, i_b;
|
36 |
|
|
input i_valid;
|
37 |
|
|
output reg [31:0] o_c;
|
38 |
|
|
output wire [3:0] o_f;
|
39 |
|
|
output reg o_valid;
|
40 |
|
|
|
41 |
|
|
wire [63:0] w_rol_tmp;
|
42 |
|
|
assign w_rol_tmp = { i_a, i_a } << i_b[4:0];
|
43 |
|
|
wire [31:0] w_rol_result;
|
44 |
|
|
assign w_rol_result = w_rol_tmp[63:32]; // Won't set flags
|
45 |
15 |
dgisselq |
wire [33:0] w_lsr_result, w_asr_result;
|
46 |
|
|
wire signed [33:0] w_ia_input;
|
47 |
|
|
assign w_ia_input = { i_a[31], i_a, 1'b0 };
|
48 |
|
|
assign w_asr_result = (|i_b[31:5])? {(34){i_a[31]}}
|
49 |
|
|
: ( w_ia_input >>> (i_b[4:0]) );// ASR
|
50 |
|
|
assign w_lsr_result = (|i_b[31:5])? 34'h00
|
51 |
|
|
: { 1'b0, i_a, 1'b0 } >> (i_b[4:0]);// LSR
|
52 |
2 |
dgisselq |
|
53 |
25 |
dgisselq |
|
54 |
|
|
wire signed [16:0] w_mpy_a_input, w_mpy_b_input;
|
55 |
|
|
wire signed [33:0] w_mpy_result;
|
56 |
|
|
assign w_mpy_a_input = { ((i_a[15])&&(i_op[2])), i_a[15:0] };
|
57 |
|
|
assign w_mpy_b_input = { ((i_b[15])&&(i_op[2])), i_b[15:0] };
|
58 |
|
|
assign w_mpy_result = w_mpy_a_input * w_mpy_b_input;
|
59 |
|
|
|
60 |
2 |
dgisselq |
wire z, n, v;
|
61 |
|
|
reg c, pre_sign, set_ovfl;
|
62 |
|
|
always @(posedge i_clk)
|
63 |
|
|
if (i_ce)
|
64 |
|
|
set_ovfl =((((i_op==4'h0)||(i_op==4'h8)) // SUB&CMP
|
65 |
|
|
&&(i_a[31] != i_b[31]))
|
66 |
|
|
||((i_op==4'ha)&&(i_a[31] == i_b[31])) // ADD
|
67 |
|
|
||(i_op == 4'hd) // LSL
|
68 |
|
|
||(i_op == 4'hf)); // LSR
|
69 |
|
|
always @(posedge i_clk)
|
70 |
|
|
if (i_ce)
|
71 |
|
|
begin
|
72 |
|
|
pre_sign <= (i_a[31]);
|
73 |
|
|
c <= 1'b0;
|
74 |
3 |
dgisselq |
casez(i_op)
|
75 |
|
|
4'b?000:{c,o_c } <= {(i_b>i_a),i_a - i_b};// CMP/SUB
|
76 |
|
|
4'b?001: o_c <= i_a & i_b; // BTST/And
|
77 |
25 |
dgisselq |
4'h3: { c, o_c } <= {1'b0,w_mpy_result[31:0]}; // MPYU/S
|
78 |
|
|
4'h4: { c, o_c } <= {1'b0,w_mpy_result[31:0]}; // MPYU/S
|
79 |
2 |
dgisselq |
4'h5: o_c <= w_rol_result; // ROL
|
80 |
|
|
4'h6: o_c <= { i_a[31:16], i_b[15:0] }; // LODILO
|
81 |
|
|
4'h7: o_c <= { i_b[15:0], i_a[15:0] }; // LODIHI
|
82 |
|
|
4'ha: { c, o_c } <= i_a + i_b; // Add
|
83 |
|
|
4'hb: o_c <= i_a | i_b; // Or
|
84 |
|
|
4'hc: o_c <= i_a ^ i_b; // Xor
|
85 |
12 |
dgisselq |
4'hd: { c, o_c } <= (|i_b[31:5])? 33'h00 : {1'b0, i_a } << i_b[4:0]; // LSL
|
86 |
15 |
dgisselq |
4'he: { o_c, c } <= w_asr_result[32:0];// ASR
|
87 |
|
|
4'hf: { o_c, c } <= w_lsr_result[32:0];// LSR
|
88 |
2 |
dgisselq |
default: o_c <= i_b; // MOV, LDI
|
89 |
|
|
endcase
|
90 |
|
|
end
|
91 |
|
|
|
92 |
|
|
assign z = (o_c == 32'h0000);
|
93 |
|
|
assign n = (o_c[31]);
|
94 |
|
|
assign v = (set_ovfl)&&(pre_sign != o_c[31]);
|
95 |
|
|
|
96 |
|
|
assign o_f = { v, n, c, z };
|
97 |
|
|
|
98 |
|
|
initial o_valid = 1'b0;
|
99 |
|
|
always @(posedge i_clk)
|
100 |
|
|
if (i_rst)
|
101 |
|
|
o_valid <= 1'b0;
|
102 |
|
|
else if (i_ce)
|
103 |
|
|
o_valid <= i_valid;
|
104 |
|
|
else if (~i_ce)
|
105 |
|
|
o_valid <= 1'b0;
|
106 |
|
|
endmodule
|