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

Subversion Repositories klc32

[/] [klc32/] [trunk/] [rtl/] [verilog/] [KLC32.v] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 robfinch
// ============================================================================
2
// (C) 2011 Robert Finch
3
// All Rights Reserved.
4
// robfinch<remove>@opencores.org
5
//
6
// KLC32.v
7
//  - 32 bit CPU
8
//
9
// This source file is free software: you can redistribute it and/or modify 
10
// it under the terms of the GNU Lesser General Public License as published 
11
// by the Free Software Foundation, either version 3 of the License, or     
12
// (at your option) any later version.                                      
13
//                                                                          
14
// This source file is distributed in the hope that it will be useful,      
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
17
// GNU General Public License for more details.                             
18
//                                                                          
19
// You should have received a copy of the GNU General Public License        
20
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
21
//                                                                          
22
// ============================================================================
23
//
24
`define STACK_VECTOR    32'h00000000
25
`define RESET_VECTOR    32'h00000004
26
`define NMI_VECTOR              32'h0000007C
27
`define IRQ_VECTOR              32'h00000064
28
`define TRAP_VECTOR             32'h00000080
29
`define TRAPV_VECTOR    32'h0000001C
30
`define TRACE_VECTOR    32'h00000024
31
`define BUS_ERR_VECTOR  32'h00000008
32
`define ILLEGAL_INSN    32'h00000010
33
`define PRIVILEGE_VIOLATION     32'h00000020
34
 
35
`define MISC    6'd0
36
`define JMP32           6'd32
37
`define JSR32           6'd33
38
`define RTS                     6'd34
39
`define RTI                     6'd35
40
`define TRACE_ON        6'd48
41
`define TRACE_OFF       6'd49
42
`define USER_MODE       6'd50
43
`define SET_IM          6'd51
44
`define RST                     6'd52
45
`define STOP            6'd53
46
`define R               6'd1
47 10 robfinch
`define ABS                     6'd1
48 2 robfinch
`define SGN                     6'd2
49
`define NEG                     6'd3
50
`define NOT                     6'd4
51
`define EXTB            6'd5
52
`define EXTH            6'd6
53 10 robfinch
`define UNLK            6'd24
54
`define MTSPR           6'd32
55
`define MFSPR           6'd33
56 2 robfinch
`define MOV_CRn2CRn     6'd48
57
`define MOV_CRn2REG     6'd49
58
`define MOV_REG2CRn     6'd50
59
`define EXEC            6'd63
60
`define RR              6'd2
61
`define ADD                     6'd4
62
`define SUB                     6'd5
63
`define CMP                     6'd6
64
`define AND                     6'd8
65
`define OR                      6'd9
66
`define EOR                     6'd10
67 10 robfinch
`define ANDC            6'd11
68 2 robfinch
`define NAND            6'd12
69
`define NOR                     6'd13
70
`define ENOR            6'd14
71 10 robfinch
`define ORC                     6'd15
72 2 robfinch
`define SHL                     6'd16
73
`define SHR                     6'd17
74
`define ROL                     6'd18
75
`define ROR                     6'd19
76
`define JMP_RR          6'd20
77
`define JSR_RR          6'd21
78
`define MAX                     6'd22
79
`define MIN                     6'd23
80
`define MULU            6'd24
81
`define MULUH           6'd25
82 10 robfinch
`define MULS            6'd26
83
`define MULSH           6'd27
84
`define DIVU            6'd28
85
`define DIVS            6'd29
86
`define MODU            6'd30
87
`define MODS            6'd31
88 2 robfinch
`define LWX                     6'd48
89
`define LHX                     6'd49
90
`define LBX                     6'd50
91
`define LHUX            6'd51
92
`define LBUX            6'd52
93
`define SWX                     6'd56
94
`define SHX                     6'd57
95
`define SBX                     6'd58
96
`define BCDADD          6'd60
97
`define BCDSUB          6'd61
98
`define RRR             6'd3
99
`define ADDI    6'd4
100
`define SUBI    6'd5
101
`define CMPI    6'd6
102
`define ANDI    6'd8
103
`define ORI             6'd9
104
`define EORI    6'd10
105 10 robfinch
`define MULUI   6'd12
106
`define MULSI   6'd13
107
`define DIVUI   6'd14
108
`define DIVSI   6'd15
109 2 robfinch
`define Bcc             6'd16
110
`define BRA                     4'd0
111
`define BRN                     4'd1
112
`define BHI                     4'd2
113
`define BLS                     4'd3
114
`define BHS                     4'd4
115
`define BLO                     4'd5
116
`define BNE                     4'd6
117
`define BEQ                     4'd7
118
`define BVC                     4'd8
119
`define BVS                     4'd9
120
`define BPL                     4'd10
121
`define BMI                     4'd11
122
`define BGE                     4'd12
123
`define BLT                     4'd13
124
`define BGT                     4'd14
125
`define BLE                     4'd15
126
`define TRAPcc  6'd17
127
`define TRAP            4'd0
128
`define TRN                     4'd1
129
`define THI                     4'd2
130
`define TLS                     4'd3
131
`define THS                     4'd4
132
`define TLO                     4'd5
133
`define TNE                     4'd6
134
`define TEQ                     4'd7
135
`define TVC                     4'd8
136
`define TVS                     4'd9
137
`define TPL                     4'd10
138
`define TMI                     4'd11
139
`define TGE                     4'd12
140
`define TLT                     4'd13
141
`define TGT                     4'd14
142
`define TLE                     4'd15
143
`define SETcc   6'd18
144
`define SET                     4'd0
145
`define STN                     4'd1
146
`define SHI                     4'd2
147
`define SLS                     4'd3
148
`define SHS                     4'd4
149
`define SLO                     4'd5
150
`define SNE                     4'd6
151
`define SEQ                     4'd7
152
`define SVC                     4'd8
153
`define SVS                     4'd9
154
`define SPL                     4'd10
155
`define SMI                     4'd11
156
`define SGE                     4'd12
157
`define SLT                     4'd13
158
`define SGT                     4'd14
159
`define SLE                     4'd15
160
`define CRxx    6'd19
161
`define ANDI_CCR        5'd8
162
`define ORI_CCR         5'd9
163
`define EORI_CCR        5'd10
164 10 robfinch
`define CROR            10'd449
165
`define CRORC           10'd417
166
`define CRAND           10'd257
167
`define CRANDC          10'd129
168
`define CRXOR           10'd193
169
`define CRNOR           10'd33
170
`define CRNAND          10'd225
171
`define CRXNOR          10'd289
172 2 robfinch
`define JMP             6'd20
173
`define JSR             6'd21
174
 
175
`define TAS             6'd46
176
`define LW              6'd48
177
`define LH              6'd49
178
`define LB              6'd50
179
`define LHU             6'd51
180
`define LBU             6'd52
181
`define POP             6'd53
182
`define LINK    6'd54
183
`define PEA             6'd55
184
`define SW              6'd56
185
`define SH              6'd57
186
`define SB              6'd58
187
`define PUSH    6'd59
188
`define NOP             6'd60
189
 
190
module KLC32(rst_i, clk_i, ipl_i, vpa_i, halt_i, inta_o, fc_o, rst_o, cyc_o, stb_o, ack_i, err_i, sel_o, we_o, adr_o, dat_i, dat_o);
191
parameter IFETCH = 8'd1;
192
parameter REGFETCHA = 8'd2;
193
parameter REGFETCHB = 8'd3;
194
parameter EXECUTE = 8'd4;
195
parameter MEMORY1 = 8'd5;
196
parameter MEMORY1_ACK = 8'd6;
197
parameter WRITEBACK = 8'd7;
198
parameter JSR1 = 8'd10;
199
parameter JSR2 = 8'd11;
200
parameter JSRShort = 8'd12;
201
parameter RTS = 8'd13;
202
parameter JMP = 8'd14;
203
parameter LOAD_SP = 8'd15;
204
parameter VECTOR = 8'd16;
205
parameter INTA = 8'd20;
206
parameter FETCH_VECTOR = 8'd21;
207
parameter TRAP1 = 8'd22;
208
parameter TRAP2 = 8'd23;
209
parameter TRAP3 = 8'd24;
210
parameter RTI1 = 8'd25;
211
parameter RTI2 = 8'd26;
212
parameter RTI3 = 8'd27;
213
parameter TRAP = 8'd28;
214
parameter RESET = 8'd29;
215
parameter JSR32 = 8'd30;
216
parameter JMP32 = 8'd31;
217
parameter WRITE_FLAGS = 8'd32;
218
parameter FETCH_IMM32 = 8'd33;
219
parameter REGFETCHC = 8'd34;
220
parameter PUSH1 = 8'd35;
221
parameter PUSH2 = 8'd36;
222
parameter PUSH3 = 8'd37;
223
parameter POP1 = 8'd38;
224
parameter POP2 = 8'd39;
225
parameter POP3 = 8'd40;
226
parameter LINK = 8'd41;
227
parameter UNLK = 8'd42;
228
parameter TAS = 8'd43;
229
parameter TAS2 = 8'd44;
230
parameter PEA = 8'd45;
231 10 robfinch
parameter MULTDIV1 = 8'd49;
232
parameter MULTDIV2 = 8'd50;
233
parameter MULT1 = 8'd51;
234
parameter MULT2 = 8'd52;
235
parameter MULT3 = 8'd53;
236
parameter MULT4 = 8'd54;
237
parameter MULT5 = 8'd55;
238
parameter MULT6 = 8'd56;
239
parameter MULT7 = 8'd57;
240
parameter DIV1 = 8'd61;
241
parameter DIV2 = 8'd62;
242 2 robfinch
input rst_i;
243
input clk_i;
244
input [2:0] ipl_i;
245
input vpa_i;
246
input halt_i;
247
output inta_o;
248
reg inta_o;
249
output [2:0] fc_o;
250
reg [2:0] fc_o;
251
output rst_o;
252
output cyc_o;
253
reg cyc_o;
254
output stb_o;
255
reg stb_o;
256
input ack_i;
257
input err_i;
258
output we_o;
259
reg we_o;
260
output [3:0] sel_o;
261
reg [3:0] sel_o;
262
output [31:0] adr_o;
263
reg [31:0] adr_o;
264
input [31:0] dat_i;
265
output [31:0] dat_o;
266
reg [31:0] dat_o;
267
 
268 10 robfinch
reg cpu_clk_en;
269
reg clk_en;
270
wire clk;
271
 
272 2 robfinch
reg [7:0] state;
273
reg [31:0] ir;
274
reg tf,sf;
275
reg [31:0] pc;
276
reg [31:0] usp,ssp;
277 10 robfinch
reg [31:0] ctr;
278 2 robfinch
wire [5:0] opcode=ir[31:26];
279 10 robfinch
reg Rcbit;
280 2 robfinch
reg [5:0] mopcode;
281
wire [5:0] func=ir[5:0];
282 10 robfinch
wire [9:0] func1=ir[10:1];
283 2 robfinch
wire [3:0] cond=ir[19:16];
284
wire [31:0] brdisp = {{16{ir[15]}},ir[15:2],2'b0};
285
reg [4:0] Rn;
286
reg [31:0] regfile [31:0];
287
wire [31:0] rfo1 = regfile[Rn];
288
wire [31:0] rfo = (Rn==5'd0) ? 32'd0 : (Rn==5'd31) ? (sf ? ssp : usp) : rfo1;
289
reg vf,nf,cf,zf;
290 10 robfinch
reg xer_ov,xer_ca,xer_so;
291 2 robfinch
reg [2:0] im;
292
reg [2:0] iplr;
293
reg [7:0] vecnum;
294
reg [31:0] vector;
295
reg [31:0] ea;
296 10 robfinch
reg [15:0] rstsh;
297
assign rst_o = rstsh[15];
298 2 robfinch
reg prev_nmi;
299
reg nmi_edge;
300
reg [31:0] sr1;
301
reg [31:0] tgt;
302 10 robfinch
reg [31:0] a,b,c,imm,aa,bb;
303 2 robfinch
wire signed [31:0] as = a;
304
wire signed [31:0] bs = b;
305
reg [31:0] res;
306
reg [3:0] cr0,cr1,cr2,cr3,cr4,cr5,cr6,cr7;
307
wire [31:0] cr = {cr7,cr6,cr5,cr4,cr3,cr2,cr1,cr0};
308
wire [31:0] sr = {tf,1'b0,sf,2'b00,im,16'd0};
309
reg [31:0] tick;
310 12 robfinch
reg [31:0] be_addr;
311 10 robfinch
 
312
reg [5:0] cnt;
313
reg [31:0] div_r0;
314
reg [31:0] div_q0;
315
reg [31:0] div_q,div_r;
316
wire [32:0] div_dif = div_r0 - bb;
317
 
318 2 robfinch
wire IsSubi = opcode==`SUBI;
319
wire IsCmpi = opcode==`CMPI;
320
wire IsSub = opcode==`RR && func==`SUB;
321
wire IsCmp = opcode==`RR && func==`CMP;
322
wire IsNeg = opcode==`R && func==`NEG;
323 10 robfinch
wire IsDivi = opcode==`DIVUI || opcode==`DIVSI;
324
wire IsDivu = opcode==`DIVUI || (opcode==`RR && (func==`DIVU || func==`MODU));
325
wire IsMult = opcode==`MULUI || opcode==`MULSI || (opcode==`RR && (func==`MULU || func==`MULS || func==`MULUH || func==`MULSH));
326
wire IsDiv = opcode==`DIVUI || opcode==`DIVSI || (opcode==`RR && (func==`DIVU || func==`DIVS || func==`MODU || func==`MODS));
327 2 robfinch
 
328
wire hasConst16 =
329
        opcode==`ADDI || opcode==`SUBI || opcode==`CMPI ||
330
        opcode==`ANDI || opcode==`ORI || opcode==`EORI ||
331
        opcode==`LW || opcode==`LH || opcode==`LB || opcode==`LHU || opcode==`LBU ||
332
        opcode==`SW || opcode==`SH || opcode==`SB ||
333
        opcode==`PEA || opcode==`TAS || opcode==`LINK
334
        ;
335
wire isStop =
336
        opcode==`MISC && (func==`STOP)
337
        ;
338
 
339
wire c_ri,c_rr;
340
wire v_ri,v_rr;
341
carry u1 (.op(IsSubi|IsCmpi), .a(a[31]), .b(imm[31]), .s(res[31]), .c(c_ri));
342
carry u2 (.op(IsSub|IsCmp|IsNeg), .a(a[31]), .b(b[31]), .s(res[31]), .c(c_rr));
343
overflow u3 (.op(IsSubi|IsCmpi), .a(a[31]), .b(imm[31]), .s(res[31]), .v(v_ri));
344
overflow u4 (.op(IsSub|IsCmp|IsNeg), .a(a[31]), .b(b[31]), .s(res[31]), .v(v_rr));
345
 
346
wire [7:0] bcdaddo,bcdsubo;
347
wire bcdaddc,bcdsubc;
348
BCDAdd u5 (.ci(cr0[0]),.a(a[7:0]),.b(b[7:0]),.o(bcdaddo),.c(bcdaddc));
349
BCDSub u6 (.ci(cr0[0]),.a(a[7:0]),.b(b[7:0]),.o(bcdsubo),.c(bcdsubc));
350
 
351
wire [63:0] shlo = {32'd0,a} << b[4:0];
352
wire [63:0] shro = {a,32'd0} >> b[4:0];
353
 
354 10 robfinch
reg res_sgn;
355
wire [31:0] mp0 = aa[15:0] * bb[15:0];
356
wire [31:0] mp1 = aa[15:0] * bb[31:16];
357
wire [31:0] mp2 = aa[31:16] * bb[15:0];
358
wire [31:0] mp3 = aa[31:16] * bb[31:16];
359
reg [63:0] prod;
360
wire divByZero;
361
 
362 2 robfinch
function GetCrBit;
363
input [4:0] Rn;
364
begin
365
        case(Rn[4:2])
366
        3'd0:   GetCrBit = cr0[Rn[1:0]];
367
        3'd1:   GetCrBit = cr1[Rn[1:0]];
368
        3'd2:   GetCrBit = cr2[Rn[1:0]];
369
        3'd3:   GetCrBit = cr3[Rn[1:0]];
370
        3'd4:   GetCrBit = cr4[Rn[1:0]];
371
        3'd5:   GetCrBit = cr5[Rn[1:0]];
372
        3'd6:   GetCrBit = cr6[Rn[1:0]];
373
        3'd7:   GetCrBit = cr7[Rn[1:0]];
374
        endcase
375
end
376
endfunction
377
 
378
function [3:0] GetCr;
379
input [2:0] Rn;
380
begin
381
        case(Rn)
382
        3'd0:   GetCr = cr0;
383
        3'd1:   GetCr = cr1;
384
        3'd2:   GetCr = cr2;
385
        3'd3:   GetCr = cr3;
386
        3'd4:   GetCr = cr4;
387
        3'd5:   GetCr = cr5;
388
        3'd6:   GetCr = cr6;
389
        3'd7:   GetCr = cr7;
390
        endcase
391
end
392
endfunction
393
 
394 10 robfinch
wire [3:0] crc = GetCr(Rn[4:2]);
395 2 robfinch
wire cr_zf = crc[2];
396
wire cr_nf = crc[3];
397
wire cr_cf = crc[0];
398
wire cr_vf = crc[1];
399
 
400
//-----------------------------------------------------------------------------
401
// Clock control
402
// - reset or NMI reenables the clock
403
// - this circuit must be under the clk_i domain
404
//-----------------------------------------------------------------------------
405
//
406
BUFGCE u20 (.CE(cpu_clk_en), .I(clk_i), .O(clk) );
407
 
408
always @(posedge clk_i)
409
if (rst_i) begin
410
        cpu_clk_en <= 1'b1;
411
end
412
else begin
413
        if (ipl_i==3'd7)
414
                cpu_clk_en <= 1'b1;
415
        else
416
                cpu_clk_en <= clk_en;
417
end
418
 
419
 
420
//-----------------------------------------------------------------------------
421
//-----------------------------------------------------------------------------
422
 
423
always @(posedge clk)
424
if (rst_i) begin
425
        prev_nmi <= 1'b0;
426
        nmi_edge <= 1'b0;
427
        state <= RESET;
428
        im <= 3'b111;
429
        sf <= 1'b1;
430
        tf <= 1'b0;
431
        inta_o <= 1'b0;
432
        cyc_o <= 1'b0;
433
        stb_o <= 1'b0;
434
        sel_o <= 4'b0000;
435
        we_o <= 1'b0;
436
        clk_en <= 1'b1;
437
        tick <= 32'd0;
438 10 robfinch
        rstsh <= 16'hFFFF;
439 2 robfinch
end
440
else begin
441
tick <= tick + 32'd1;
442
clk_en <= 1'b1;
443 10 robfinch
rstsh <= {rstsh,1'b0};
444 2 robfinch
prev_nmi <= ipl_i==3'd7;
445
if (!prev_nmi && (ipl_i==3'd7))
446
        nmi_edge <= 1'b1;
447
 
448
case(state)
449
`include "RESET.v"
450
`include "VECTOR.v"
451
`include "IFETCH.v"
452
 
453
`include "REGFETCHA.v"
454
`include "REGFETCHB.v"
455
`include "REGFETCHC.v"
456
`include "FETCH_IMM32.v"
457
`include "EXECUTE.v"
458
`include "MEMORY.v"
459
`include "PUSH.v"
460
`include "POP.v"
461
`include "WRITEBACK.v"
462
`include "WRITE_FLAGS.v"
463
 
464
`include "JMP.v"
465
`include "JSR.v"
466
`include "RTS.v"
467
`include "INTA.v"
468
`include "TRAP.v"
469
`include "RTI.v"
470
 
471 10 robfinch
`include "MULTDIV.v"
472
 
473 2 robfinch
endcase
474 12 robfinch
 
475
`include "bus_error.v"
476
 
477 2 robfinch
end
478
 
479
endmodule

powered by: WebSVN 2.1.0

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