OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [core/] [cpuops.v] - Blame information for rev 69

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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 56 dgisselq
                        o_illegal);
37
        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 2 dgisselq
 
47 62 dgisselq
        // Rotate-left pre-logic
48 2 dgisselq
        wire    [63:0]   w_rol_tmp;
49
        assign  w_rol_tmp = { i_a, i_a } << i_b[4:0];
50
        wire    [31:0]   w_rol_result;
51
        assign  w_rol_result = w_rol_tmp[63:32]; // Won't set flags
52 62 dgisselq
 
53
        // Shift register pre-logic
54 56 dgisselq
        wire    [32:0]           w_lsr_result, w_asr_result;
55
        assign  w_asr_result = (|i_b[31:5])? {(33){i_a[31]}}
56
                                : ( {i_a, 1'b0 } >>> (i_b[4:0]) );// ASR
57
        assign  w_lsr_result = (|i_b[31:5])? 33'h00
58
                                : ( { i_a, 1'b0 } >> (i_b[4:0]) );// LSR
59 2 dgisselq
 
60 69 dgisselq
        // Bit reversal pre-logic
61
        wire    [31:0]   w_brev_result;
62
        genvar  k;
63
        generate
64
        for(k=0; k<32; k=k+1)
65
                assign w_brev_result[k] = i_b[31-k];
66
        endgenerate
67 25 dgisselq
 
68 69 dgisselq
        // Popcount pre-logic
69
        wire    [31:0]   w_popc_result;
70
        assign  w_popc_result[5:0]=
71
                 ({5'h0,i_b[ 0]}+{5'h0,i_b[ 1]}+{5'h0,i_b[ 2]}+{5'h0,i_b[ 3]})
72
                +({5'h0,i_b[ 4]}+{5'h0,i_b[ 5]}+{5'h0,i_b[ 6]}+{5'h0,i_b[ 7]})
73
                +({5'h0,i_b[ 8]}+{5'h0,i_b[ 9]}+{5'h0,i_b[10]}+{5'h0,i_b[11]})
74
                +({5'h0,i_b[12]}+{5'h0,i_b[13]}+{5'h0,i_b[14]}+{5'h0,i_b[15]})
75
                +({5'h0,i_b[16]}+{5'h0,i_b[17]}+{5'h0,i_b[18]}+{5'h0,i_b[19]})
76
                +({5'h0,i_b[20]}+{5'h0,i_b[21]}+{5'h0,i_b[22]}+{5'h0,i_b[23]})
77
                +({5'h0,i_b[24]}+{5'h0,i_b[25]}+{5'h0,i_b[26]}+{5'h0,i_b[27]})
78
                +({5'h0,i_b[28]}+{5'h0,i_b[29]}+{5'h0,i_b[30]}+{5'h0,i_b[31]});
79
        assign  w_popc_result[31:6] = 26'h00;
80
 
81
        // Prelogic for our flags registers
82 2 dgisselq
        wire    z, n, v;
83
        reg     c, pre_sign, set_ovfl;
84
        always @(posedge i_clk)
85 69 dgisselq
                if (i_ce) // 1 LUT
86
                        set_ovfl =(((i_op==4'h0)&&(i_a[31] != i_b[31]))//SUB&CMP
87
                                ||((i_op==4'h2)&&(i_a[31] == i_b[31])) // ADD
88
                                ||(i_op == 4'h6) // LSL
89
                                ||(i_op == 4'h5)); // LSR
90 56 dgisselq
 
91 62 dgisselq
 
92
        // A 4-way multiplexer can be done in one 6-LUT.
93
        // A 16-way multiplexer can therefore be done in 4x 6-LUT's with
94
        //      the Xilinx multiplexer fabric that follows. 
95
        // Given that we wish to apply this multiplexer approach to 33-bits,
96
        // this will cost a minimum of 132 6-LUTs.
97 56 dgisselq
        generate
98
        if (IMPLEMENT_MPY == 0)
99
        begin
100
                always @(posedge i_clk)
101 2 dgisselq
                if (i_ce)
102
                begin
103
                        pre_sign <= (i_a[31]);
104
                        c <= 1'b0;
105 3 dgisselq
                        casez(i_op)
106 69 dgisselq
                        4'b0000:{c,o_c } <= {1'b0,i_a}-{1'b0,i_b};// CMP/SUB
107
                        4'b0001:   o_c   <= i_a & i_b;          // BTST/And
108
                        4'b0010:{c,o_c } <= i_a + i_b;          // Add
109
                        4'b0011:   o_c   <= i_a | i_b;          // Or
110
                        4'b0100:   o_c   <= i_a ^ i_b;          // Xor
111
                        4'b0101:{o_c,c } <= w_lsr_result[32:0];  // LSR
112
                        4'b0110:{c,o_c } <= (|i_b[31:5])? 33'h00 : {1'b0, i_a } << i_b[4:0];     // LSL
113
                        4'b0111:{o_c,c } <= w_asr_result[32:0];  // ASR
114
                        4'b1000:   o_c   <= { i_b[15: 0], i_a[15:0] }; // LODIHI
115
                        4'b1001:   o_c   <= { i_a[31:16], i_b[15:0] }; // LODILO
116
                        // 4'h1010: The unimplemented MPYU,
117
                        // 4'h1011: and here for the unimplemented MPYS
118
                        4'b1100:   o_c   <= w_brev_result;      // BREV
119
                        4'b1101:   o_c   <= w_popc_result;      // POPC
120
                        4'b1110:   o_c   <= w_rol_result;       // ROL
121
                        default:   o_c   <= i_b;                // MOV, LDI
122 56 dgisselq
                        endcase
123
                end
124
        end else begin
125 62 dgisselq
                //
126
                // Multiply pre-logic
127
                //
128 69 dgisselq
                wire    signed_mpy;
129
                assign  signed_mpy = i_op[0];
130 56 dgisselq
                wire    signed  [16:0]   w_mpy_a_input, w_mpy_b_input;
131
                wire    signed  [33:0]   w_mpy_result;
132 69 dgisselq
                assign  w_mpy_a_input ={ ((i_a[15])&&(signed_mpy)), i_a[15:0] };
133
                assign  w_mpy_b_input ={ ((i_b[15])&&(signed_mpy)), i_b[15:0] };
134 56 dgisselq
                assign  w_mpy_result  = w_mpy_a_input * w_mpy_b_input;
135
 
136 62 dgisselq
 
137
                //
138
                // The master ALU case statement
139
                //
140 56 dgisselq
                always @(posedge i_clk)
141
                if (i_ce)
142
                begin
143
                        pre_sign <= (i_a[31]);
144
                        c <= 1'b0;
145
                        casez(i_op)
146 69 dgisselq
                        4'b0000:{c,o_c } <= {1'b0,i_a}-{1'b0,i_b};// CMP/SUB
147
                        4'b0001:   o_c   <= i_a & i_b;          // BTST/And
148
                        4'b0010:{c,o_c } <= i_a + i_b;          // Add
149
                        4'b0011:   o_c   <= i_a | i_b;          // Or
150
                        4'b0100:   o_c   <= i_a ^ i_b;          // Xor
151
                        4'b0101:{o_c,c } <= w_lsr_result[32:0];  // LSR
152
                        4'b0110:{c,o_c } <= (|i_b[31:5])? 33'h00 : {1'b0, i_a } << i_b[4:0];     // LSL
153
                        4'b0111:{o_c,c } <= w_asr_result[32:0];  // ASR
154
                        4'b1000:   o_c   <= { i_b[15: 0], i_a[15:0] }; // LODIHI
155
                        4'b1001:   o_c   <= { i_a[31:16], i_b[15:0] }; // LODILO
156
                        4'b1010:{c,o_c } <= {1'b0,w_mpy_result[31:0]}; // MPYU
157
                        4'b1011:{c,o_c } <= {1'b0,w_mpy_result[31:0]}; // MPYS
158
                        4'b1100:   o_c   <= w_brev_result;      // BREV
159
                        4'b1101:   o_c   <= w_popc_result;      // POPC
160
                        4'b1110:   o_c   <= w_rol_result;       // ROL
161
                        default:   o_c   <= i_b;                // MOV, LDI
162 2 dgisselq
                        endcase
163
                end
164 56 dgisselq
        end endgenerate
165 2 dgisselq
 
166 56 dgisselq
        generate
167
        if (IMPLEMENT_MPY == 0)
168
        begin
169
                reg     r_illegal;
170
                always @(posedge i_clk)
171 62 dgisselq
                        r_illegal <= (i_ce)&&((i_op == 4'h3)||(i_op == 4'h4));
172 56 dgisselq
                assign o_illegal = r_illegal;
173
        end else
174
                assign o_illegal = 1'b0;
175
        endgenerate
176
 
177 2 dgisselq
        assign  z = (o_c == 32'h0000);
178
        assign  n = (o_c[31]);
179
        assign  v = (set_ovfl)&&(pre_sign != o_c[31]);
180
 
181
        assign  o_f = { v, n, c, z };
182
 
183
        initial o_valid = 1'b0;
184
        always @(posedge i_clk)
185
                if (i_rst)
186
                        o_valid <= 1'b0;
187 56 dgisselq
                else
188
                        o_valid <= (i_ce)&&(i_valid);
189 2 dgisselq
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2022 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.