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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [cpu/] [cpuops.v] - Blame information for rev 46

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

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

powered by: WebSVN 2.1.0

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