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

Subversion Repositories ao68000

[/] [ao68000/] [trunk/] [rtl/] [ao68000.v] - Blame information for rev 15

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

Line No. Rev Author Line
1 12 alfik
/*
2
 * Copyright 2010, Aleksander Osman, alfik@poczta.fm. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without modification, are
5
 * permitted provided that the following conditions are met:
6
 *
7
 *  1. Redistributions of source code must retain the above copyright notice, this list of
8
 *     conditions and the following disclaimer.
9
 *
10
 *  2. Redistributions in binary form must reproduce the above copyright notice, this list
11
 *     of conditions and the following disclaimer in the documentation and/or other materials
12
 *     provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 */
24
 
25
/*! \file ao68000.v
26
 * \brief Main ao68000 IP Core source file.
27
 */
28
 
29
/***********************************************************************************************************************
30
 * Definitions of microcode operations - parsed by ao68000_tool to generate the defines in the section below
31
 **********************************************************************************************************************/
32
// OPERATIONS START
33
`define EA_REG_IDLE                                         3'd0
34
`define EA_REG_IR_2_0                                       3'd1
35
`define EA_REG_IR_11_9                                      3'd2
36
`define EA_REG_MOVEM_REG_2_0                                3'd3
37
`define EA_REG_3b111                                        3'd4
38
`define EA_REG_3b100                                        3'd5
39
 
40
`define EA_MOD_IDLE                                         4'd0
41
`define EA_MOD_IR_5_3                                       4'd1
42
`define EA_MOD_MOVEM_MOD_5_3                                4'd2
43
`define EA_MOD_IR_8_6                                       4'd3
44
`define EA_MOD_PREDEC                                       4'd4    // predecrement:    -(An)
45
`define EA_MOD_3b111                                        4'd5    // extended mod
46
`define EA_MOD_DN_PREDEC                                    4'd6    // MOD.DN_PREDEC: Dn 3'b000 (ir[3] == 1'b0), -(An) 3'b100 (ir[3] == 1'b1)
47
`define EA_MOD_DN_AN_EXG                                    4'd7    // MOD.DN_AN_EXG: Dn 3'b000 (ir[7:3] == 5'b01000 or 5'b10001), An 3'b001 (ir[7:3] == 5'b01001)
48
`define EA_MOD_POSTINC                                      4'd8    // MOD.POSTINC: postincrement (An)+ 3'b011
49
`define EA_MOD_AN                                           4'd9    // MOD.AN: An 3'b001, saved result is sign-extended
50
`define EA_MOD_DN                                           4'd10   // MOD.DN: Dn 3'b000
51
`define EA_MOD_INDIRECTOFFSET                               4'd11   // MOD.INDIRECTOFFSET: (d16, An) 3'b101
52
 
53
`define EA_TYPE_IDLE                                        4'd0
54
`define EA_TYPE_ALL                                         4'd1    // TYPE.ALL: all
55
`define EA_TYPE_CONTROL_POSTINC                             4'd2    // TYPE.CONTROL_POSTINC: control or postincrement
56
`define EA_TYPE_CONTROLALTER_PREDEC                         4'd3    // TYPE.CONTROLALTER_PREDEC: control alter or predecrement
57
`define EA_TYPE_CONTROL                                     4'd4    // TYPE.CONTROL: control
58
`define EA_TYPE_DATAALTER                                   4'd5    // TYPE.DATAALTER: data alter
59
`define EA_TYPE_DN_AN                                       4'd6    // TYPE.DN_AN: Dn, An
60
`define EA_TYPE_MEMORYALTER                                 4'd7    // TYPE.MEMORYALTER: memory alter
61
`define EA_TYPE_DATA                                        4'd8    // TYPE.DATA: data
62
 
63
`define OP1_IDLE                                            4'd0
64
`define OP1_FROM_OP2                                        4'd1    // move from operand2
65
`define OP1_FROM_ADDRESS                                    4'd2    // move from address
66
`define OP1_FROM_DATA                                       4'd3    // move from data, sign extend
67
`define OP1_FROM_IMMEDIATE                                  4'd4    // move immediate, sign extend
68
`define OP1_FROM_RESULT                                     4'd5    // move from result
69
`define OP1_MOVEQ                                           4'd6    // move moveq: { 24{ir[7]}, ir[7:0] }
70
`define OP1_FROM_PC                                         4'd7    // move from PC
71
`define OP1_LOAD_ZEROS                                      4'd8    // load zeros:  32'b0
72
`define OP1_LOAD_ONES                                       4'd9    // load ones:   32'hFFFFFFFF
73
`define OP1_FROM_SR                                         4'd10   // move from SR
74
`define OP1_FROM_USP                                        4'd11   // move from USP
75
`define OP1_FROM_AN                                         4'd12   // move from An, 32 bits
76
`define OP1_FROM_DN                                         4'd13   // move from Dn, sign extend
77
`define OP1_FROM_IR                                         4'd14   // move from ir[15:0]
78
`define OP1_FROM_FAULT_ADDRESS                              4'd15   // move from fault_address
79
 
80
`define OP2_IDLE                                            3'd0
81
`define OP2_FROM_OP1                                        3'd1    // move from operand1
82
`define OP2_LOAD_1                                          3'd2    // load: 32'b1
83
`define OP2_LOAD_COUNT                                      3'd3    // load count
84
`define OP2_ADDQ_SUBQ                                       3'd4    // load addq_subq
85
`define OP2_MOVE_OFFSET                                     3'd5    // move offset
86
`define OP2_MOVE_ADDRESS_BUS_INFO                           3'd6    // move address_bus_info
87
`define OP2_DECR_BY_1                                       3'd7    // decrement by 1
88
 
89
`define ADDRESS_IDLE                                        4'd0
90
`define ADDRESS_INCR_BY_SIZE                                4'd1    // increment by size
91
`define ADDRESS_DECR_BY_SIZE                                4'd2    // decrement by size
92
`define ADDRESS_INCR_BY_2                                   4'd3    // increment by 2
93
`define ADDRESS_FROM_AN_OUTPUT                              4'd4    // move from An output
94
`define ADDRESS_FROM_BASE_INDEX_OFFSET                      4'd5    // move from base+index+offset
95
`define ADDRESS_FROM_IMM_16                                 4'd6    // move from {16{ir1[15]}, ir1[15:0]}
96
`define ADDRESS_FROM_IMM_32                                 4'd7    // move from {ir1[15:0], ir2[15:0]}
97
`define ADDRESS_FROM_PC_INDEX_OFFSET                        4'd8    // move from pc+index+offset
98
`define ADDRESS_FROM_TRAP                                   4'd9    // move trap {22'b0, trap[7:0], 2'b0}
99
 
100
`define SIZE_IDLE                                           4'd0
101 15 alfik
`define SIZE_BYTE                                           4'd1    // load byte: 3'b001
102
`define SIZE_WORD                                           4'd2    // load word: 3'b010
103
`define SIZE_LONG                                           4'd3    // load long: 3'b100
104 12 alfik
`define SIZE_1                                              4'd4    // SIZE.1: word ( ir[7:6] == 2'b00 ), long ( ir[7:6] == 2'b01 )
105
`define SIZE_1_PLUS                                         4'd5    // SIZE.1+: word ( ir[7:6] == 2'b10 ), long ( ir[7:6] == 2'b11 )
106
`define SIZE_2                                              4'd6    // SIZE.2: word ( ir[6] == 1'b0 ), long ( ir[6] == 1'b1 )
107
`define SIZE_3                                              4'd7    // SIZE.3: byte ( ir[7:6] == 2'b00 ), word ( ir[7:6] == 2'b01 ), long ( ir[7:6] == 2'b10 )
108
`define SIZE_4                                              4'd8    // SIZE.4: byte ( ir[13:12] == 2'b01 ), word( ir[13:12] == 2'b11 ), long ( ir[13:12] == 2'b10 )
109
`define SIZE_5                                              4'd9    // SIZE.5: word ( ir[8] == 1'b0 ), long ( ir[8] == 1'b1 )
110
`define SIZE_6                                              4'd10   // SIZE.6: byte ( ir[5:3] != 3'b000 ), long ( ir[5:3] == 3'b000 )
111
 
112
`define MOVEM_MODREG_IDLE                                   3'd0
113
`define MOVEM_MODREG_LOAD_0                                 3'd1    // load 6'b0
114
`define MOVEM_MODREG_LOAD_6b001111                          3'd2    // load 6'b001111
115
`define MOVEM_MODREG_INCR_BY_1                              3'd3    // increment by 1
116
`define MOVEM_MODREG_DECR_BY_1                              3'd4    // decrement by 1
117
 
118
`define MOVEM_LOOP_IDLE                                     2'd0
119
`define MOVEM_LOOP_LOAD_0                                   2'd1    // load 4'b0
120
`define MOVEM_LOOP_INCR_BY_1                                2'd2    // increment by 1
121
 
122
`define MOVEM_REG_IDLE                                      2'd0
123
`define MOVEM_REG_FROM_OP1                                  2'd1    // load from operand1[15:0]
124
`define MOVEM_REG_SHIFT_RIGHT                               2'd2    // shift right
125
 
126
`define IR_IDLE                                             2'd0
127
`define IR_LOAD_WHEN_PREFETCH_VALID                         2'd1    // load from prefetch_ir[79:64]
128
 
129
`define PC_IDLE                                             3'd0
130
`define PC_FROM_RESULT                                      3'd1    // move from result
131
`define PC_INCR_BY_2                                        3'd2    // increment by 2
132
`define PC_INCR_BY_4                                        3'd3    // increment by 4
133 13 alfik
`define PC_INCR_BY_SIZE                                     3'd4    // increment by size: 2 (size == 3'b001 || size == 3'b010), 4 (size == 3'b100)
134 12 alfik
`define PC_FROM_PREFETCH_IR                                 3'd5    // move from prefetch_ir
135
`define PC_INCR_BY_2_IN_MAIN_LOOP                           3'd6    // increment by 2, in main loop, when valid prefetch and valid instruction
136
 
137
`define TRAP_IDLE                                           4'd0
138
`define TRAP_ILLEGAL_INSTR                                  4'd1    // move illegal_instr:  8'd4
139
`define TRAP_DIV_BY_ZERO                                    4'd2    // move divide_by_zero: 8'd5
140
`define TRAP_CHK                                            4'd3    // move chk:            8'd6
141
`define TRAP_TRAPV                                          4'd4    // move trapv:          8'd7
142
`define TRAP_PRIVIL_VIOLAT                                  4'd5    // move priv_viol:      8'd8
143
`define TRAP_TRACE                                          4'd6    // move trace:          8'd9
144
`define TRAP_TRAP                                           4'd7    // move trap:           { 3'b0, 1'b1, ir[3:0] }
145
`define TRAP_FROM_DECODER                                   4'd8    // move from decoder_trap
146
`define TRAP_FROM_INTERRUPT                                 4'd9    // move from interrupt_trap
147
 
148
`define OFFSET_IDLE                                         2'd0
149
`define OFFSET_IMM_8                                        2'd1    // { 24{ir1[7]}, ir1[7:0] }
150
`define OFFSET_IMM_16                                       2'd2    // { 16{ir1[15]}, ir1[15:0] }
151
 
152
`define INDEX_IDLE                                          2'd0
153
`define INDEX_0                                             2'd1    // 32'b0
154
`define INDEX_LOAD_EXTENDED                                 2'd2    // load from extended instruction word
155
 
156
`define STOP_FLAG_IDLE                                      2'd0
157
`define STOP_FLAG_SET                                       2'd1    // set, continue when: trace,interrupt or reset
158
`define STOP_FLAG_CLEAR                                     2'd2    // clear
159
 
160
`define TRACE_FLAG_IDLE                                     2'd0
161
`define TRACE_FLAG_COPY_WHEN_NO_STOP                        2'd1    // remember trace bit, move from sr[15]
162
 
163
`define GROUP_0_FLAG_IDLE                                   2'd0
164
`define GROUP_0_FLAG_SET                                    2'd1    // set, processing group zero exception
165
`define GROUP_0_FLAG_CLEAR_WHEN_VALID_PREFETCH              2'd2    // clear
166
 
167
`define INSTRUCTION_FLAG_IDLE                               2'd0
168
`define INSTRUCTION_FLAG_SET                                2'd1    // set, processing instruction
169
`define INSTRUCTION_FLAG_CLEAR_IN_MAIN_LOOP                 2'd2    // clear, in main loop, when valid prefetch and valid instruction
170
 
171
`define READ_MODIFY_WRITE_FLAG_IDLE                         2'd0
172
`define READ_MODIFY_WRITE_FLAG_SET                          2'd1    // set, execute a RMW cycle
173
`define READ_MODIFY_WRITE_FLAG_CLEAR                        2'd2    // clear
174
 
175
`define DO_RESET_FLAG_IDLE                                  2'd0
176
`define DO_RESET_FLAG_SET                                   2'd1    // set, signal reset
177
`define DO_RESET_FLAG_CLEAR                                 2'd2    // clear
178
 
179
`define DO_INTERRUPT_FLAG_IDLE                              2'd0
180
`define DO_INTERRUPT_FLAG_SET_IF_ACTIVE                     2'd1    // set if interrupt active
181
`define DO_INTERRUPT_FLAG_CLEAR                             2'd2    // clear
182
 
183
`define DO_READ_FLAG_IDLE                                   2'd0
184
`define DO_READ_FLAG_SET                                    2'd1    // set, perform read operation
185
`define DO_READ_FLAG_CLEAR                                  2'd2    // clear
186
 
187
`define DO_WRITE_FLAG_IDLE                                  2'd0
188
`define DO_WRITE_FLAG_SET                                   2'd1    // set, perform write operation
189
`define DO_WRITE_FLAG_CLEAR                                 2'd2    // clear
190
 
191
`define DO_BLOCKED_FLAG_IDLE                                2'd0
192
`define DO_BLOCKED_FLAG_SET                                 2'd1    // set, block processor
193
 
194
`define DATA_WRITE_IDLE                                     2'd0
195
`define DATA_WRITE_FROM_RESULT                              2'd1    // load data write register from result register
196
 
197
`define AN_ADDRESS_IDLE                                     2'd0    // load from ea_reg, user or supervisor
198
`define AN_ADDRESS_FROM_EXTENDED                            2'd1    // load from extended instruction word: ir1[14:12], user or supervisor
199
`define AN_ADDRESS_USP                                      2'd2    // load USP address
200
`define AN_ADDRESS_SSP                                      2'd3    // load SSP address
201
 
202
`define AN_WRITE_ENABLE_IDLE                                1'd0
203
`define AN_WRITE_ENABLE_SET                                 1'd1    // set write enable on An register
204
 
205
`define AN_INPUT_IDLE                                       2'd0    // load from result
206
`define AN_INPUT_FROM_ADDRESS                               2'd1    // load from address
207
`define AN_INPUT_FROM_PREFETCH_IR                           2'd2    // load from prefetch_ir, for reset, for SSP
208
 
209
`define DN_ADDRESS_IDLE                                     1'd0    // load from ea_reg
210
`define DN_ADDRESS_FROM_EXTENDED                            1'd1    // load from extended instruction word: ir1[14:12]
211
 
212
`define DN_WRITE_ENABLE_IDLE                                1'd0
213
`define DN_WRITE_ENABLE_SET                                 1'd1    // set write enable on Dn register
214
 
215
`define ALU_IDLE                                            5'd0
216
`define ALU_SR_SET_INTERRUPT                                5'd1
217
`define ALU_SR_SET_TRAP                                     5'd2
218
`define ALU_MOVEP_M2R_1                                     5'd3
219
`define ALU_MOVEP_M2R_2                                     5'd4
220
`define ALU_MOVEP_M2R_3                                     5'd5
221
`define ALU_MOVEP_M2R_4                                     5'd6
222
`define ALU_MOVEP_R2M_1                                     5'd7
223
`define ALU_MOVEP_R2M_2                                     5'd8
224
`define ALU_MOVEP_R2M_3                                     5'd9
225
`define ALU_MOVEP_R2M_4                                     5'd10
226
`define ALU_SIGN_EXTEND                                     5'd11
227
`define ALU_ARITHMETIC_LOGIC                                5'd12
228
`define ALU_ABCD_SBCD_ADDX_SUBX                             5'd13
229
`define ALU_ASL_LSL_ROL_ROXL_ASR_LSR_ROR_ROXR_prepare       5'd14
230
`define ALU_ASL_LSL_ROL_ROXL_ASR_LSR_ROR_ROXR               5'd15
231
`define ALU_MOVE                                            5'd16
232
`define ALU_ADDA_SUBA_CMPA_ADDQ_SUBQ                        5'd17
233
`define ALU_CHK                                             5'd18
234
`define ALU_MULS_MULU_DIVS_DIVU                             5'd19
235
`define ALU_BCHG_BCLR_BSET_BTST                             5'd20
236
`define ALU_TAS                                             5'd21
237
`define ALU_NEGX_CLR_NEG_NOT_NBCD_SWAP_EXT                  5'd22
238
`define ALU_SIMPLE_LONG_ADD                                 5'd23
239
`define ALU_SIMPLE_LONG_SUB                                 5'd24
240
`define ALU_MOVE_TO_CCR_SR_RTE_RTR_STOP_LOGIC_TO_CCR_SR     5'd25
241
`define ALU_SIMPLE_MOVE                                     5'd26
242
`define ALU_LINK_MOVE                                       5'd27
243
 
244
`define BRANCH_IDLE                                         4'd0
245
`define BRANCH_movem_loop                                   4'd1    // BRANCH(movem_loop == 4'b1000)
246
`define BRANCH_movem_reg                                    4'd2    // BRANCH(movem_reg[0] == 0)
247
`define BRANCH_operand2                                     4'd3    // BRANCH(operand2[5:0] == 6'b0)
248 13 alfik
`define BRANCH_alu_signal                                   4'd4    // BRANCH(alu_signal == 1'b0)
249
`define BRANCH_alu_mult_div_ready                           4'd5    // BRANCH(alu_mult_div_ready == 1'b1)
250 12 alfik
`define BRANCH_condition_0                                  4'd6    // BRANCH(condition == 1'b0)
251
`define BRANCH_condition_1                                  4'd7    // BRANCH(condition == 1'b1)
252
`define BRANCH_result                                       4'd8    // BRANCH(result[15:0] == 16'hFFFF)
253
`define BRANCH_V                                            4'd9    // BRANCH(V == 1'b0)
254
`define BRANCH_movep_16                                     4'd10   // BRANCH(ir[6] == 0)
255
`define BRANCH_stop_flag_wait_ir_decode                     4'd11   // BRANCH(stop_flag == 1'b1) if no branch: wait for prefetch ir valid and decode instruction
256
`define BRANCH_ir                                           4'd12   // BRANCH(ir[7:0] != 8'b0)
257
`define BRANCH_trace_flag_and_interrupt                     4'd13   // BRANCH(trace_flag == 1'b0 && interrupt_mask != 3'b000) if no branch: jump to main loop
258
`define BRANCH_group_0_flag                                 4'd14   // BRANCH(group_0_flag == 0)
259
`define BRANCH_procedure                                    4'd15   // call procedure, return from procedure
260
 
261
`define PROCEDURE_IDLE                                      4'd0
262
`define PROCEDURE_call_load_ea                              4'd1    // load ea
263
`define PROCEDURE_call_perform_ea_read                      4'd2    // perform_ea_read
264
`define PROCEDURE_call_perform_ea_write                     4'd3    // perform_ea_write
265
`define PROCEDURE_call_save_ea                              4'd4    // save ea
266
`define PROCEDURE_return                                    4'd5    // return from procedure
267
`define PROCEDURE_wait_finished                             4'd6    // wait for finished signal from bus controler
268
`define PROCEDURE_wait_prefetch_valid                       4'd7    // wait for prefetch ir valid, 64 bits
269
`define PROCEDURE_wait_prefetch_valid_32                    4'd8    // wait for prefetch ir valid, 32 bits
270
`define PROCEDURE_jump_to_main_loop                         4'd9    // jump to main loop
271
`define PROCEDURE_push_micropc                              4'd10   // save current micro_pc
272
`define PROCEDURE_call_trap                                 4'd11   // call trap service procedure
273
`define PROCEDURE_pop_micropc                               4'd12   // pop most recent micro_pc and forget
274
`define PROCEDURE_interrupt_mask                            4'd13   // if interrupt active continue, else jump to main loop
275
`define PROCEDURE_call_read                                 4'd14   // load_ea + perform_ea_read
276
`define PROCEDURE_call_write                                4'd15   // perform_ea_write + save_ea + return
277
// OPERATIONS END
278
 
279
/***********************************************************************************************************************
280
 * Automatically generated by ao68000_tool microcode word bit assignments and addresses
281
 **********************************************************************************************************************/
282
// MICROCODE - DO NOT EDIT BELOW
283
`define MICRO_DATA_ea_reg                                                            micro_data[2:0]
284
`define MICRO_DATA_ea_mod                                                            micro_data[6:3]
285
`define MICRO_DATA_ea_type                                                           micro_data[10:7]
286
`define MICRO_DATA_op1                                                               micro_data[14:11]
287
`define MICRO_DATA_op2                                                               micro_data[17:15]
288
`define MICRO_DATA_address                                                           micro_data[21:18]
289
`define MICRO_DATA_size                                                              micro_data[25:22]
290
`define MICRO_DATA_movem_modreg                                                      micro_data[28:26]
291
`define MICRO_DATA_movem_loop                                                        micro_data[30:29]
292
`define MICRO_DATA_movem_reg                                                         micro_data[32:31]
293
`define MICRO_DATA_ir                                                                micro_data[34:33]
294
`define MICRO_DATA_pc                                                                micro_data[37:35]
295
`define MICRO_DATA_trap                                                              micro_data[41:38]
296
`define MICRO_DATA_offset                                                            micro_data[43:42]
297
`define MICRO_DATA_index                                                             micro_data[45:44]
298
`define MICRO_DATA_stop_flag                                                         micro_data[47:46]
299
`define MICRO_DATA_trace_flag                                                        micro_data[49:48]
300
`define MICRO_DATA_group_0_flag                                                      micro_data[51:50]
301
`define MICRO_DATA_instruction_flag                                                  micro_data[53:52]
302
`define MICRO_DATA_read_modify_write_flag                                            micro_data[55:54]
303
`define MICRO_DATA_do_reset_flag                                                     micro_data[57:56]
304
`define MICRO_DATA_do_interrupt_flag                                                 micro_data[59:58]
305
`define MICRO_DATA_do_read_flag                                                      micro_data[61:60]
306
`define MICRO_DATA_do_write_flag                                                     micro_data[63:62]
307
`define MICRO_DATA_do_blocked_flag                                                   micro_data[65:64]
308
`define MICRO_DATA_data_write                                                        micro_data[67:66]
309
`define MICRO_DATA_an_address                                                        micro_data[69:68]
310
`define MICRO_DATA_an_write_enable                                                   micro_data[70:70]
311
`define MICRO_DATA_an_input                                                          micro_data[72:71]
312
`define MICRO_DATA_dn_address                                                        micro_data[73:73]
313
`define MICRO_DATA_dn_write_enable                                                   micro_data[74:74]
314
`define MICRO_DATA_alu                                                               micro_data[79:75]
315
`define MICRO_DATA_branch                                                            micro_data[83:80]
316
`define MICRO_DATA_procedure                                                         micro_data[87:84]
317
 
318
`define MICROPC_MOVE                                                                 9'd231
319 13 alfik
`define MICROPC_MOVE_USP_to_An                                                       9'd400
320
`define MICROPC_TAS                                                                  9'd332
321
`define MICROPC_BSR                                                                  9'd430
322 12 alfik
`define MICROPC_ADDRESS_BUS_TRAP                                                     9'd3
323
`define MICROPC_MOVEP_register_to_memory                                             9'd106
324 13 alfik
`define MICROPC_NEGX_CLR_NEG_NOT_NBCD                                                9'd337
325
`define MICROPC_RTS                                                                  9'd471
326 12 alfik
`define MICROPC_MAIN_LOOP                                                            9'd53
327
`define MICROPC_ADDA_SUBA                                                            9'd268
328 13 alfik
`define MICROPC_MOVE_TO_CCR_MOVE_TO_SR                                               9'd391
329
`define MICROPC_MOVE_FROM_SR                                                         9'd388
330 12 alfik
`define MICROPC_LOAD_EA_d8_PC_Xn                                                     9'd79
331
`define MICROPC_TRAP_ENTRY                                                           9'd35
332
`define MICROPC_PERFORM_EA_READ_memory                                               9'd89
333 13 alfik
`define MICROPC_RESET                                                                9'd485
334 12 alfik
`define MICROPC_PERFORM_EA_WRITE_Dn                                                  9'd91
335
`define MICROPC_ASL_LSL_ROL_ROXL_ASR_LSR_ROR_ROXR_all_memory                         9'd225
336
`define MICROPC_MOVEA                                                                9'd239
337 13 alfik
`define MICROPC_TST                                                                  9'd344
338
`define MICROPC_BTST_register                                                        9'd326
339 12 alfik
`define MICROPC_LOAD_EA_d8_An_Xn                                                     9'd68
340
`define MICROPC_MULS_MULU_DIVS_DIVU                                                  9'd290
341 13 alfik
`define MICROPC_MOVEQ                                                                9'd307
342 12 alfik
`define MICROPC_CMPA                                                                 9'd275
343
`define MICROPC_EOR                                                                  9'd245
344
`define MICROPC_LOAD_EA_xxx_W                                                        9'd72
345 13 alfik
`define MICROPC_DBcc                                                                 9'd374
346 12 alfik
`define MICROPC_CMPI                                                                 9'd184
347
`define MICROPC_LOAD_EA_xxx_L                                                        9'd74
348
`define MICROPC_CMPM                                                                 9'd205
349 13 alfik
`define MICROPC_MOVE_USP_to_USP                                                      9'd395
350
`define MICROPC_ADDQ_SUBQ_not_An                                                     9'd348
351
`define MICROPC_ULNK                                                                 9'd419
352 12 alfik
`define MICROPC_EXG                                                                  9'd197
353
`define MICROPC_ADD_to_mem_SUB_to_mem_AND_to_mem_OR_to_mem                           9'd250
354 13 alfik
`define MICROPC_Bcc_BRA                                                              9'd362
355 12 alfik
`define MICROPC_PERFORM_EA_READ_An                                                   9'd86
356
`define MICROPC_LOAD_EA_d16_PC                                                       9'd76
357 13 alfik
`define MICROPC_NOP                                                                  9'd479
358 12 alfik
`define MICROPC_MOVEM_register_to_memory_predecrement                                9'd131
359 13 alfik
`define MICROPC_RTE_RTR                                                              9'd459
360
`define MICROPC_TRAP                                                                 9'd480
361
`define MICROPC_ADDQ_SUBQ_An                                                         9'd351
362 12 alfik
`define MICROPC_MOVEM_register_to_memory_control                                     9'd147
363 13 alfik
`define MICROPC_BTST_immediate                                                       9'd315
364 12 alfik
`define MICROPC_MOVEP_memory_to_register                                             9'd98
365
`define MICROPC_PERFORM_EA_WRITE_An                                                  9'd92
366
`define MICROPC_CHK                                                                  9'd281
367 13 alfik
`define MICROPC_Scc                                                                  9'd355
368
`define MICROPC_JMP                                                                  9'd442
369 12 alfik
`define MICROPC_PEA                                                                  9'd168
370
`define MICROPC_SAVE_EA_minus_An                                                     9'd97
371
`define MICROPC_ANDI_EORI_ORI_ADDI_SUBI                                              9'd174
372 13 alfik
`define MICROPC_BCHG_BCLR_BSET_immediate                                             9'd310
373 12 alfik
`define MICROPC_LOAD_EA_An                                                           9'd62
374
`define MICROPC_PERFORM_EA_READ_imm                                                  9'd87
375
`define MICROPC_ADD_to_Dn_SUB_to_Dn_AND_to_Dn_OR_to_Dn                               9'd255
376
`define MICROPC_LEA                                                                  9'd162
377 13 alfik
`define MICROPC_TRAPV                                                                9'd482
378
`define MICROPC_LINK                                                                 9'd403
379 12 alfik
`define MICROPC_ABCD_SBCD_ADDX_SUBX                                                  9'd189
380 13 alfik
`define MICROPC_BCHG_BCLR_BSET_register                                              9'd321
381 12 alfik
`define MICROPC_PERFORM_EA_READ_Dn                                                   9'd85
382
`define MICROPC_LOAD_EA_illegal_command                                              9'd83
383
`define MICROPC_ORI_to_CCR_ORI_to_SR_ANDI_to_CCR_ANDI_to_SR_EORI_to_CCR_EORI_to_SR   9'd178
384
`define MICROPC_CMP                                                                  9'd262
385 13 alfik
`define MICROPC_SWAP_EXT                                                             9'd340
386
`define MICROPC_STOP                                                                 9'd488
387 12 alfik
`define MICROPC_PERFORM_EA_WRITE_memory                                              9'd93
388 13 alfik
`define MICROPC_JSR                                                                  9'd450
389 12 alfik
`define MICROPC_LOAD_EA_minus_An                                                     9'd63
390
`define MICROPC_ASL_LSL_ROL_ROXL_ASR_LSR_ROR_ROXR_all_immediate_register             9'd212
391
`define MICROPC_SAVE_EA_An_plus                                                      9'd95
392
`define MICROPC_LOAD_EA_d16_An                                                       9'd65
393
`define MICROPC_LOAD_EA_An_plus                                                      9'd62
394
`define MICROPC_MOVEM_memory_to_register                                             9'd116
395
// MICROCODE - DO NOT EDIT ABOVE
396
 
397
/***********************************************************************************************************************
398
 * ao68000 top level module
399
 **********************************************************************************************************************/
400
 
401
/*! \brief ao68000 top level module.
402
 *
403
 * This module contains only instantiations of sub-modules and wire declarations.
404
 */
405
module ao68000 (
406
    //****************** WISHBONE
407
    input           CLK_I,              //% \copydoc CLK_I
408
    input           reset_n,            //% \copydoc reset_n
409
 
410
    output          CYC_O,              //% \copydoc CYC_O
411
    output  [31:2]  ADR_O,              //% \copydoc ADR_O
412
    output  [31:0]  DAT_O,              //% \copydoc DAT_O
413
    input   [31:0]  DAT_I,              //% \copydoc DAT_I
414
    output  [3:0]   SEL_O,              //% \copydoc SEL_O
415
    output          STB_O,              //% \copydoc STB_O
416
    output          WE_O,               //% \copydoc WE_O
417
 
418
    input           ACK_I,              //% \copydoc ACK_I
419
    input           ERR_I,              //% \copydoc ERR_I
420
    input           RTY_I,              //% \copydoc RTY_I
421
 
422
    // TAG_TYPE: TGC_O
423
    output          SGL_O,              //% \copydoc SGL_O
424
    output          BLK_O,              //% \copydoc BLK_O
425
    output          RMW_O,              //% \copydoc RMW_O
426
 
427
    // TAG_TYPE: TGA_O
428
    output [2:0]    CTI_O,              //% \copydoc CTI_O
429
    output [1:0]    BTE_O,              //% \copydoc BTE_O
430
 
431
    // TAG_TYPE: TGC_O
432
    output [2:0]    fc_o,               //% \copydoc fc_o
433
 
434
    //****************** OTHER
435
    /* interrupt acknowlege:
436
     * ACK_I: interrupt vector on DAT_I[7:0]
437
     * ERR_I: spurious interrupt
438
     * RTY_I: autovector
439
     */
440
    input [2:0]     ipl_i,              //% \copydoc ipl_i
441
    output          reset_o,            //% \copydoc reset_o
442
    output          blocked_o           //% \copydoc blocked_o
443
);
444
 
445
wire [15:0] sr;
446 13 alfik
wire [2:0]  size;
447 12 alfik
wire [31:0] address;
448
wire        address_type;
449
wire        read_modify_write_flag;
450
wire [31:0] data_read;
451
wire [31:0] data_write;
452
wire [31:0] pc;
453
wire        prefetch_ir_valid;
454
wire [79:0] prefetch_ir;
455
wire        do_reset;
456
wire        do_read;
457
wire        do_write;
458
wire        do_interrupt;
459
wire        do_blocked;
460
wire        jmp_address_trap;
461
wire        jmp_bus_trap;
462
wire        finished;
463
wire [7:0]  interrupt_trap;
464
wire [2:0]  interrupt_mask;
465
wire        rw_state;
466
wire [2:0]  fc_state;
467
wire [7:0]  decoder_trap;
468
wire [31:0] usp;
469
wire [31:0] Dn_output;
470
wire [31:0] An_output;
471
wire [31:0] result;
472
wire [3:0]  An_address;
473
wire [31:0] An_input;
474
wire [2:0]  Dn_address;
475
wire [15:0] ir;
476
wire [8:0]  decoder_micropc;
477 13 alfik
wire        alu_signal;
478
wire        alu_mult_div_ready;
479 12 alfik
wire [8:0]  load_ea;
480
wire [8:0]  perform_ea_read;
481
wire [8:0]  perform_ea_write;
482
wire [8:0]  save_ea;
483
wire        trace_flag;
484
wire        group_0_flag;
485
wire        stop_flag;
486
wire [8:0]  micro_pc;
487
wire [31:0] operand1;
488
wire [31:0] operand2;
489
wire [4:0]  movem_loop;
490
wire [15:0] movem_reg;
491
wire        condition;
492
wire [87:0] micro_data;
493
wire [31:0] fault_address_state;
494
wire [1:0]  pc_change;
495
wire        prefetch_ir_valid_32;
496
wire [3:0]  ea_type;
497
wire [2:0]  ea_mod;
498
wire [2:0]  ea_reg;
499
 
500
bus_control bus_control_m(
501
    .CLK_I                  (CLK_I),
502
    .reset_n                (reset_n),
503
    .CYC_O                  (CYC_O),
504
    .ADR_O                  (ADR_O),
505
    .DAT_O                  (DAT_O),
506
    .DAT_I                  (DAT_I),
507
    .SEL_O                  (SEL_O),
508
    .STB_O                  (STB_O),
509
    .WE_O                   (WE_O),
510
    .ACK_I                  (ACK_I),
511
    .ERR_I                  (ERR_I),
512
    .RTY_I                  (RTY_I),
513
    .SGL_O                  (SGL_O),
514
    .BLK_O                  (BLK_O),
515
    .RMW_O                  (RMW_O),
516
    .CTI_O                  (CTI_O),
517
    .BTE_O                  (BTE_O),
518
    .fc_o                   (fc_o),
519
    .ipl_i                  (ipl_i),
520
    .reset_o                (reset_o),
521
    .blocked_o              (blocked_o),
522
 
523
    .supervisor_i           (sr[13]),
524
    .ipm_i                  (sr[10:8]),
525
    .size_i                 (size),
526
    .address_i              (address),
527
    .address_type_i         (address_type),
528
    .read_modify_write_i    (read_modify_write_flag),
529
    .data_write_i           (data_write),
530
    .data_read_o            (data_read),
531
    .pc_i                   (pc),
532
    .pc_change_i            (pc_change),
533
    .prefetch_ir_o          (prefetch_ir),
534
    .prefetch_ir_valid_32_o (prefetch_ir_valid_32),
535
    .prefetch_ir_valid_o    (prefetch_ir_valid),
536
    .prefetch_ir_valid_80_o (),
537
    .do_reset_i             (do_reset),
538
    .do_blocked_i           (do_blocked),
539
    .do_read_i              (do_read),
540
    .do_write_i             (do_write),
541
    .do_interrupt_i         (do_interrupt),
542
    .jmp_address_trap_o     (jmp_address_trap),
543
    .jmp_bus_trap_o         (jmp_bus_trap),
544
    .finished_o             (finished),
545
    .interrupt_trap_o       (interrupt_trap),
546
    .interrupt_mask_o       (interrupt_mask),
547
    .rw_state_o             (rw_state),
548
    .fc_state_o             (fc_state),
549
    .fault_address_state_o  (fault_address_state)
550
);
551
 
552
registers registers_m(
553
    .clock                          (CLK_I),
554
    .reset_n                        (reset_n),
555
    .data_read                      (data_read),
556
    .prefetch_ir                    (prefetch_ir),
557
    .prefetch_ir_valid              (prefetch_ir_valid),
558
    .result                         (result),
559
    .sr                             (sr),
560
    .rw_state                       (rw_state),
561
    .fc_state                       (fc_state),
562
    .fault_address_state            (fault_address_state),
563
    .interrupt_trap                 (interrupt_trap),
564
    .interrupt_mask                 (interrupt_mask),
565
    .decoder_trap                   (decoder_trap),
566
    .usp                            (usp),
567
    .Dn_output                      (Dn_output),
568
    .An_output                      (An_output),
569
 
570
    .pc_change                      (pc_change),
571
 
572
    .ea_reg                         (ea_reg),
573
    .ea_reg_control                 (`MICRO_DATA_ea_reg),
574
    .ea_mod                         (ea_mod),
575
    .ea_mod_control                 (`MICRO_DATA_ea_mod),
576
    .ea_type                        (ea_type),
577
    .ea_type_control                (`MICRO_DATA_ea_type),
578
    .operand1                       (operand1),
579
    .operand1_control               (`MICRO_DATA_op1),
580
    .operand2                       (operand2),
581
    .operand2_control               (`MICRO_DATA_op2),
582
    .address                        (address),
583
    .address_type                   (address_type),
584
    .address_control                (`MICRO_DATA_address),
585
    .size                           (size),
586
    .size_control                   (`MICRO_DATA_size),
587
    .movem_modreg                   (),
588
    .movem_modreg_control           (`MICRO_DATA_movem_modreg),
589
    .movem_loop                     (movem_loop),
590
    .movem_loop_control             (`MICRO_DATA_movem_loop),
591
    .movem_reg                      (movem_reg),
592
    .movem_reg_control              (`MICRO_DATA_movem_reg),
593
    .ir                             (ir),
594
    .ir_control                     (`MICRO_DATA_ir),
595
    .pc                             (pc),
596
    .pc_control                     (`MICRO_DATA_pc),
597
    .trap                           (),
598
    .trap_control                   (`MICRO_DATA_trap),
599
    .offset                         (),
600
    .offset_control                 (`MICRO_DATA_offset),
601
    .index                          (),
602
    .index_control                  (`MICRO_DATA_index),
603
    .stop_flag                      (stop_flag),
604
    .stop_flag_control              (`MICRO_DATA_stop_flag),
605
    .trace_flag                     (trace_flag),
606
    .trace_flag_control             (`MICRO_DATA_trace_flag),
607
    .group_0_flag                   (group_0_flag),
608
    .group_0_flag_control           (`MICRO_DATA_group_0_flag),
609
    .instruction_flag               (),
610
    .instruction_flag_control       (`MICRO_DATA_instruction_flag),
611
    .read_modify_write_flag         (read_modify_write_flag),
612
    .read_modify_write_flag_control (`MICRO_DATA_read_modify_write_flag),
613
    .do_reset_flag                  (do_reset),
614
    .do_reset_flag_control          (`MICRO_DATA_do_reset_flag),
615
    .do_interrupt_flag              (do_interrupt),
616
    .do_interrupt_flag_control      (`MICRO_DATA_do_interrupt_flag),
617
    .do_read_flag                   (do_read),
618
    .do_read_flag_control           (`MICRO_DATA_do_read_flag),
619
    .do_write_flag                  (do_write),
620
    .do_write_flag_control          (`MICRO_DATA_do_write_flag),
621
    .do_blocked_flag                (do_blocked),
622
    .do_blocked_flag_control        (`MICRO_DATA_do_blocked_flag),
623
    .data_write                     (data_write),
624
    .data_write_control             (`MICRO_DATA_data_write),
625
    .An_address                     (An_address),
626
    .An_address_control             (`MICRO_DATA_an_address),
627
    .An_input                       (An_input),
628
    .An_input_control               (`MICRO_DATA_an_input),
629
    .Dn_address                     (Dn_address),
630
    .Dn_address_control             (`MICRO_DATA_dn_address)
631
);
632
 
633
memory_registers memory_registers_m(
634
    .clock              (CLK_I),
635
    .reset_n            (reset_n),
636
    .An_address         (An_address),
637
    .An_input           (An_input),
638
    .An_write_enable    (`MICRO_DATA_an_write_enable),
639
    .An_output          (An_output),
640
    .usp                (usp),
641
    .Dn_address         (Dn_address),
642
    .Dn_input           (result),
643
    .Dn_write_enable    (`MICRO_DATA_dn_write_enable),
644
    .Dn_size            (size),
645
    .Dn_output          (Dn_output),
646
    .micro_pc           (micro_pc),
647
    .micro_data         (micro_data)
648
);
649
 
650
decoder decoder_m(
651
    .clock              (CLK_I),
652
    .reset_n            (reset_n),
653
    .supervisor         (sr[13]),
654
    .ir                 (prefetch_ir[79:64]),
655
    .decoder_trap       (decoder_trap),
656
    .decoder_micropc    (decoder_micropc),
657
 
658
    .load_ea            (load_ea),
659
    .perform_ea_read    (perform_ea_read),
660
    .perform_ea_write   (perform_ea_write),
661
    .save_ea            (save_ea),
662
 
663
    .ea_type            (ea_type),
664
    .ea_mod             (ea_mod),
665
    .ea_reg             (ea_reg)
666
);
667
 
668
condition condition_m(
669
    .cond               (ir[11:8]),
670
    .ccr                (sr[7:0]),
671
    .condition          (condition)
672
);
673
 
674
alu alu_m(
675
    .clock              (CLK_I),
676
    .reset_n            (reset_n),
677
    .address            (address),
678
    .ir                 (ir),
679
    .size               (size),
680
    .operand1           (operand1),
681
    .operand2           (operand2),
682
    .interrupt_mask     (interrupt_mask),
683
    .alu_control        (`MICRO_DATA_alu),
684
    .sr                 (sr),
685
    .result             (result),
686 13 alfik
    .alu_signal         (alu_signal),
687
    .alu_mult_div_ready (alu_mult_div_ready)
688 12 alfik
);
689
 
690
microcode_branch microcode_branch_m(
691
    .clock                  (CLK_I),
692
    .reset_n                (reset_n),
693
    .movem_loop             (movem_loop),
694
    .movem_reg              (movem_reg),
695
    .operand2               (operand2),
696 13 alfik
    .alu_signal             (alu_signal),
697
    .alu_mult_div_ready     (alu_mult_div_ready),
698 12 alfik
    .condition              (condition),
699
    .result                 (result),
700
    .overflow               (sr[1]),
701
    .stop_flag              (stop_flag),
702
    .ir                     (ir),
703
    .decoder_trap           (decoder_trap),
704
    .trace_flag             (trace_flag),
705
    .group_0_flag           (group_0_flag),
706
    .interrupt_mask         (interrupt_mask),
707
    .load_ea                (load_ea),
708
    .perform_ea_read        (perform_ea_read),
709
    .perform_ea_write       (perform_ea_write),
710
    .save_ea                (save_ea),
711
    .decoder_micropc        (decoder_micropc),
712
    .prefetch_ir_valid_32   (prefetch_ir_valid_32),
713
    .prefetch_ir_valid      (prefetch_ir_valid),
714
    .jmp_address_trap       (jmp_address_trap),
715
    .jmp_bus_trap           (jmp_bus_trap),
716
    .finished               (finished),
717
    .branch_control         (`MICRO_DATA_branch),
718
    .branch_offset          (`MICRO_DATA_procedure),
719
    .micro_pc               (micro_pc)
720
);
721
 
722
endmodule
723
 
724
/***********************************************************************************************************************
725
 * Bus control
726
 **********************************************************************************************************************/
727
 
728
/*! \brief Initiate WISHBONE MASTER bus cycles.
729
 *
730
 * The bus_control module is the only module that has contact with signals from outside of the IP core.
731
 * It is responsible for initiating WISHBONE MASTER bus cycles. The cycles can be divided into:
732
 *  - memory read cycles (supervisor data, supervisor program, user data, user program)
733
 *  - memory write cycles (supervisor data, user data),
734
 *  - interrupt acknowledge.
735
 *
736
 * Every cycle is supplemented with the following tags:
737
 *  - standard WISHBONE cycle tags: SGL_O, BLK_O, RMW_O,
738
 *  - register feedback WISHBONE address tags: CTI_O and BTE_O,
739
 *  - ao68000 specific cycle tag: fc_o which is equivalent to  MC68000 function codes.
740
 *
741
 * The bus_control module is also responsible for registering interrupt inputs and initiating the interrupt acknowledge
742
 * cycle in response to a microcode request. Microcode requests a interrupt acknowledge at the end of instruction
743
 * processing, when the interrupt privilege level is higher than the current interrupt privilege mask, as specified
744
 * in the MC68000 User's Manual.
745
 *
746
 * Finally, bus_control controls also two ao68000 specific core outputs:
747
 *  - blocked output,  high when that the processor is blocked after encountering a double bus error. The only way
748
 *    to leave this block state is by reseting the ao68000 by the asynchronous reset input signal.
749
 *  - reset output, high when processing the RESET instruction. Can be used to reset external devices.
750
 */
751
module bus_control(
752
    //******************************************* external
753
    //****************** WISHBONE
754
    input CLK_I,
755
    input reset_n,
756
 
757
    output reg CYC_O,
758
    output reg [31:2] ADR_O,
759
    output reg [31:0] DAT_O,
760
    input [31:0] DAT_I,
761
    output reg [3:0] SEL_O,
762
    output reg STB_O,
763
    output reg WE_O,
764
 
765
    input ACK_I,
766
    input ERR_I,
767
    input RTY_I,
768
 
769
    // TAG_TYPE: TGC_O
770
    output reg SGL_O,
771
    output reg BLK_O,
772
    output reg RMW_O,
773
 
774
    // TAG_TYPE: TGA_O
775
    output reg [2:0] CTI_O,
776
    output [1:0] BTE_O,
777
 
778
    // TAG_TYPE: TGC_O
779
    output reg [2:0] fc_o,
780
 
781
    //****************** OTHER
782
    input [2:0] ipl_i,
783
    output reg reset_o = 1'b0,
784
    output reg blocked_o = 1'b0,
785
 
786
    //******************************************* internal
787
    input supervisor_i,
788
    input [2:0] ipm_i,
789 13 alfik
    input [2:0] size_i,
790 12 alfik
    input [31:0] address_i,
791
    input address_type_i,
792
    input read_modify_write_i,
793
    input [31:0] data_write_i,
794
    output reg [31:0] data_read_o,
795
 
796
    input [31:0] pc_i,
797
    input [1:0] pc_change_i,
798
    output reg [79:0] prefetch_ir_o,
799
    output reg prefetch_ir_valid_32_o = 1'b0,
800
    output reg prefetch_ir_valid_o = 1'b0,
801
    output reg prefetch_ir_valid_80_o = 1'b0,
802
 
803
    input do_reset_i,
804
    input do_blocked_i,
805
    input do_read_i,
806
    input do_write_i,
807
    input do_interrupt_i,
808
 
809
    output reg jmp_address_trap_o = 1'b0,
810
    output reg jmp_bus_trap_o = 1'b0,
811
    // read/write/interrupt
812
    output reg finished_o,
813
 
814
    output reg [7:0] interrupt_trap_o = 8'b0,
815
    output reg [2:0] interrupt_mask_o = 3'b0,
816
 
817
    /* mask==0 && trap==0            nothing
818
     * mask!=0                        interrupt with spurious interrupt
819
     */
820
 
821
    // write = 0/read = 1
822
    output reg rw_state_o,
823
    output reg [2:0] fc_state_o,
824
    output reg [31:0] fault_address_state_o
825
);
826
 
827
assign BTE_O = 2'b00;
828
 
829
wire [31:0] pc_i_plus_6;
830
assign pc_i_plus_6 = pc_i + 32'd6;
831
wire [31:0] pc_i_plus_4;
832
assign pc_i_plus_4 = pc_i + 32'd4;
833
 
834
wire [31:0] address_i_plus_4;
835
assign address_i_plus_4 = address_i + 32'd4;
836
 
837
reg [1:0] saved_pc_change = 2'b00;
838
 
839
parameter [4:0]
840
    S_INIT      = 5'd0,
841
    S_RESET     = 5'd1,
842
    S_BLOCKED   = 5'd2,
843
    S_INT_1     = 5'd3,
844
    S_READ_1    = 5'd4,
845
    S_READ_2    = 5'd5,
846
    S_READ_3    = 5'd6,
847
    S_WAIT      = 5'd7,
848
    S_WRITE_1   = 5'd8,
849
    S_WRITE_2   = 5'd9,
850
    S_WRITE_3   = 5'd10,
851
    S_PC_0      = 5'd11,
852
    S_PC_1      = 5'd12,
853
    S_PC_2      = 5'd13,
854
    S_PC_3      = 5'd14,
855
    S_PC_4      = 5'd15,
856
    S_PC_5      = 5'd16,
857
    S_PC_6      = 5'd17;
858
 
859
parameter [2:0]
860
    FC_USER_DATA            = 3'd1,
861
    FC_USER_PROGRAM         = 3'd2,
862
    FC_SUPERVISOR_DATA      = 3'd5,        // all exception vector entries except reset
863
    FC_SUPERVISOR_PROGRAM   = 3'd6,        // exception vector for reset
864
    FC_CPU_SPACE            = 3'd7;        // interrupt acknowlege bus cycle
865
 
866
parameter [2:0]
867
    CTI_CLASSIC_CYCLE       = 3'd0,
868
    CTI_CONST_CYCLE         = 3'd1,
869
    CTI_INCR_CYCLE          = 3'd2,
870
    CTI_END_OF_BURST        = 3'd7;
871
 
872
parameter [7:0]
873
    VECTOR_BUS_TRAP         = 8'd2,
874
    VECTOR_ADDRESS_TRAP     = 8'd3;
875
 
876
reg [4:0] current_state;
877
reg [7:0] reset_counter;
878
 
879
reg [2:0] last_interrupt_mask;
880
always @(posedge CLK_I or negedge reset_n) begin
881
    if(reset_n == 1'b0) begin
882
        interrupt_mask_o <= 3'b000;
883
        last_interrupt_mask <= 3'b000;
884
    end
885
    else if(ipl_i > ipm_i && do_interrupt_i == 1'b0) begin
886
        interrupt_mask_o <= ipl_i;
887
        last_interrupt_mask <= interrupt_mask_o;
888
    end
889
    else if(do_interrupt_i == 1'b1) begin
890
        interrupt_mask_o <= last_interrupt_mask;
891
    end
892
    else begin
893
        interrupt_mask_o <= 3'b000;
894
        last_interrupt_mask <= 3'b000;
895
    end
896
end
897
 
898
// change pc_i in middle of prefetch operation: undefined
899
 
900
always @(posedge CLK_I or negedge reset_n) begin
901
    if(reset_n == 1'b0) begin
902
        current_state <= S_INIT;
903
        interrupt_trap_o <= 8'd0;
904
        prefetch_ir_valid_o <= 1'b0;
905
        prefetch_ir_valid_32_o <= 1'b0;
906
        prefetch_ir_valid_80_o <= 1'b0;
907
 
908
        jmp_address_trap_o <= 1'b0;
909
        jmp_bus_trap_o <= 1'b0;
910
 
911
        CYC_O <= 1'b0;
912
        ADR_O <= 30'd0;
913
        DAT_O <= 32'd0;
914
        SEL_O <= 4'b0;
915
        STB_O <= 1'b0;
916
        WE_O <= 1'b0;
917
        SGL_O <= 1'b0;
918
        BLK_O <= 1'b0;
919
        RMW_O <= 1'b0;
920
        CTI_O <= 3'd0;
921
        fc_o <= 3'd0;
922
        reset_o <= 1'b0;
923
        blocked_o <= 1'b0;
924
        data_read_o <= 32'd0;
925
        finished_o <= 1'b0;
926
        rw_state_o <= 1'b0;
927
        fc_state_o <= 3'd0;
928
        fault_address_state_o <= 32'd0;
929
        saved_pc_change <= 2'b0;
930
        reset_counter <= 8'd0;
931
    end
932
    else begin
933
        case(current_state)
934
            S_INIT: begin
935
                finished_o <= 1'b0;
936
                jmp_address_trap_o <= 1'b0;
937
                jmp_bus_trap_o <= 1'b0;
938
                reset_o <= 1'b0;
939
                blocked_o <= 1'b0;
940
 
941
                // block
942
                if(do_blocked_i == 1'b1) begin
943
                    blocked_o <= 1'b1;
944
                    current_state <= S_BLOCKED;
945
                end
946
                // reset
947
                else if(do_reset_i == 1'b1) begin
948
                    reset_o <= 1'b1;
949
                    reset_counter <= 8'd124;
950
                    current_state <= S_RESET;
951
                end
952
                // read
953
                else if(do_read_i == 1'b1) begin
954
                    WE_O <= 1'b0;
955
                    if(supervisor_i == 1'b1)    fc_o <= (address_type_i == 1'b0) ? FC_SUPERVISOR_DATA : FC_SUPERVISOR_PROGRAM;
956
                    else                        fc_o <= (address_type_i == 1'b0) ? FC_USER_DATA : FC_USER_PROGRAM;
957
 
958 13 alfik
                    if(address_i[0] == 1'b1 && (size_i[0] == 1'b0)) begin // WORD or LONG WORD
959 12 alfik
                        fault_address_state_o <= address_i;
960
                        rw_state_o <= 1'b1;
961
                        fc_state_o <= (supervisor_i == 1'b1) ?  ((address_type_i == 1'b0) ? FC_SUPERVISOR_DATA : FC_SUPERVISOR_PROGRAM) :
962
                                                                ((address_type_i == 1'b0) ? FC_USER_DATA : FC_USER_PROGRAM);
963
                        interrupt_trap_o <= VECTOR_ADDRESS_TRAP;
964
 
965
                        jmp_address_trap_o <= 1'b1;
966
                        current_state <= S_WAIT;
967
                    end
968
                    else begin
969
                        CYC_O <= 1'b1;
970
                        ADR_O <= address_i[31:2];
971 13 alfik
                        SEL_O <=    (size_i[0] == 1'b1 && address_i[1:0] == 2'b00)? 4'b1000 :
972
                                    (size_i[0] == 1'b1 && address_i[1:0] == 2'b01)? 4'b0100 :
973
                                    (size_i[0] == 1'b1 && address_i[1:0] == 2'b10)? 4'b0010 :
974
                                    (size_i[0] == 1'b1 && address_i[1:0] == 2'b11)? 4'b0001 :
975
                                    (size_i[1] == 1'b1 && address_i[1] == 2'b0)?    4'b1100 :
976
                                    (size_i[0] == 1'b0 && address_i[1] == 2'b1)?    4'b0011 :
977
                                                                                    4'b1111;
978 12 alfik
                        STB_O <= 1'b1;
979
 
980
                        if(read_modify_write_i == 1'b1) begin
981
                            SGL_O <= 1'b0;
982
                            BLK_O <= 1'b0;
983
                            RMW_O <= 1'b1;
984
                            CTI_O <= CTI_END_OF_BURST;
985
                        end
986 13 alfik
                        else if(address_i[1:0] == 2'b10 && size_i[2] == 1'b1) begin
987 12 alfik
                            SGL_O <= 1'b0;
988
                            BLK_O <= 1'b1;
989
                            RMW_O <= 1'b0;
990
                            CTI_O <= CTI_INCR_CYCLE;
991
                        end
992
                        else begin
993
                            SGL_O <= 1'b1;
994
                            BLK_O <= 1'b0;
995
                            RMW_O <= 1'b0;
996
                            CTI_O <= CTI_END_OF_BURST;
997
                        end
998
 
999
                        current_state <= S_READ_1;
1000
                    end
1001
                end
1002
                // write
1003
                else if(do_write_i == 1'b1) begin
1004
                    WE_O <= 1'b1;
1005
                    if(supervisor_i == 1'b1)    fc_o <= FC_SUPERVISOR_DATA;
1006
                    else                        fc_o <= FC_USER_DATA;
1007
 
1008 13 alfik
                    if(address_i[0] == 1'b1 && size_i[0] == 1'b0) begin // WORD or LONG WORD
1009 12 alfik
                        fault_address_state_o <= address_i;
1010
                        rw_state_o <= 1'b0;
1011
                        fc_state_o <= (supervisor_i == 1'b1) ? FC_SUPERVISOR_DATA : FC_USER_DATA;
1012
                        interrupt_trap_o <= VECTOR_ADDRESS_TRAP;
1013
 
1014
                        jmp_address_trap_o <= 1'b1;
1015
                        current_state <= S_WAIT;
1016
                    end
1017
                    else begin
1018
                        CYC_O <= 1'b1;
1019
                        ADR_O <= address_i[31:2];
1020
                        STB_O <= 1'b1;
1021
 
1022 13 alfik
                        if(address_i[1:0] == 2'b10 && size_i[2] == 1'b1) begin
1023 12 alfik
                            DAT_O <= { 16'b0, data_write_i[31:16] };
1024
                            SEL_O <= 4'b0011;
1025
                        end
1026 13 alfik
                        else if(address_i[1:0] == 2'b00 && size_i[2] == 1'b1) begin
1027 12 alfik
                            DAT_O <= data_write_i[31:0];
1028
                            SEL_O <= 4'b1111;
1029
                        end
1030 13 alfik
                        else if(address_i[1:0] == 2'b10 && size_i[1] == 1'b1) begin
1031 12 alfik
                            DAT_O <= { 16'b0, data_write_i[15:0] };
1032
                            SEL_O <= 4'b0011;
1033
                        end
1034 13 alfik
                        else if(address_i[1:0] == 2'b00 && size_i[1] == 1'b1) begin
1035 12 alfik
                            DAT_O <= { data_write_i[15:0], 16'b0 };
1036
                            SEL_O <= 4'b1100;
1037
                        end
1038 13 alfik
                        else if(address_i[1:0] == 2'b11 && size_i[0] == 1'b1) begin
1039 12 alfik
                            DAT_O <= { 24'b0, data_write_i[7:0] };
1040
                            SEL_O <= 4'b0001;
1041
                        end
1042 13 alfik
                        else if(address_i[1:0] == 2'b10 && size_i[0] == 1'b1) begin
1043 12 alfik
                            DAT_O <= { 16'b0, data_write_i[7:0], 8'b0 };
1044
                            SEL_O <= 4'b0010;
1045
                        end
1046 13 alfik
                        else if(address_i[1:0] == 2'b01 && size_i[0] == 1'b1) begin
1047 12 alfik
                            DAT_O <= { 8'b0, data_write_i[7:0], 16'b0 };
1048
                            SEL_O <= 4'b0100;
1049
                        end
1050 13 alfik
                        else if(address_i[1:0] == 2'b00 && size_i[0] == 1'b1) begin
1051 12 alfik
                            DAT_O <= { data_write_i[7:0], 24'b0 };
1052
                            SEL_O <= 4'b1000;
1053
                        end
1054
 
1055
                        if(read_modify_write_i == 1'b1) begin
1056
                            SGL_O <= 1'b0;
1057
                            BLK_O <= 1'b0;
1058
                            RMW_O <= 1'b1;
1059
                            CTI_O <= CTI_END_OF_BURST;
1060
                        end
1061 13 alfik
                        else if(address_i[1:0] == 2'b10 && size_i[2] == 1'b1) begin
1062 12 alfik
                            SGL_O <= 1'b0;
1063
                            BLK_O <= 1'b1;
1064
                            RMW_O <= 1'b0;
1065
                            CTI_O <= CTI_INCR_CYCLE;
1066
                        end
1067
                        else begin
1068
                            SGL_O <= 1'b1;
1069
                            BLK_O <= 1'b0;
1070
                            RMW_O <= 1'b0;
1071
                            CTI_O <= CTI_END_OF_BURST;
1072
                        end
1073
 
1074
                        current_state <= S_WRITE_1;
1075
                    end
1076
                end
1077
                // pc
1078
                else if(prefetch_ir_valid_o == 1'b0 || pc_change_i != 2'b00) begin
1079
 
1080
                    if(prefetch_ir_valid_o == 1'b0 || pc_change_i == 2'b10 || pc_change_i == 2'b11) begin
1081
                        // load 4 words: [79:16] in 2,3 cycles
1082
                        prefetch_ir_valid_32_o <= 1'b0;
1083
                        prefetch_ir_valid_o <= 1'b0;
1084
                        prefetch_ir_valid_80_o <= 1'b0;
1085
 
1086
                        current_state <= S_PC_0;
1087
                    end
1088
                    else if(prefetch_ir_valid_80_o == 1'b0 && pc_change_i == 2'b01) begin
1089
                        // load 2 words: [31:0] in 1 cycle
1090
                        prefetch_ir_valid_32_o <= 1'b1;
1091
                        prefetch_ir_valid_o <= 1'b0;
1092
                        prefetch_ir_valid_80_o <= 1'b0;
1093
 
1094
                        prefetch_ir_o <= { prefetch_ir_o[63:0], 16'b0 };
1095
                        current_state <= S_PC_0;
1096
                    end
1097
                    else begin
1098
                        // do not load any words
1099
                        prefetch_ir_valid_32_o <= 1'b1;
1100
                        prefetch_ir_valid_o <= 1'b1;
1101
                        prefetch_ir_valid_80_o <= 1'b0;
1102
 
1103
                        prefetch_ir_o <= { prefetch_ir_o[63:0], 16'b0 };
1104
                    end
1105
 
1106
 
1107
                end
1108
                // interrupt
1109
                else if(do_interrupt_i == 1'b1) begin
1110
                    CYC_O <= 1'b1;
1111
                    ADR_O <= { 27'b111_1111_1111_1111_1111_1111_1111, last_interrupt_mask };
1112
                    SEL_O <= 4'b1111;
1113
                    STB_O <= 1'b1;
1114
                    WE_O <= 1'b0;
1115
 
1116
                    SGL_O <= 1'b1;
1117
                    BLK_O <= 1'b0;
1118
                    RMW_O <= 1'b0;
1119
                    CTI_O <= CTI_END_OF_BURST;
1120
 
1121
                    fc_o <= FC_CPU_SPACE;
1122
 
1123
                    current_state <= S_INT_1;
1124
                end
1125
            end
1126
 
1127
            S_RESET: begin
1128
                reset_counter <= reset_counter - 8'd1;
1129
 
1130
                if(reset_counter == 8'd0) begin
1131
                    finished_o <= 1'b1;
1132
                    current_state <= S_WAIT;
1133
                end
1134
            end
1135
 
1136
            S_BLOCKED: begin
1137
            end
1138
 
1139
            S_INT_1: begin
1140
                if(ACK_I == 1'b1) begin
1141
                    CYC_O <= 1'b0;
1142
                    STB_O <= 1'b0;
1143
 
1144
                    interrupt_trap_o <= DAT_I[7:0];
1145
 
1146
                    finished_o <= 1'b1;
1147
                    current_state <= S_WAIT;
1148
                end
1149
                else if(RTY_I == 1'b1) begin
1150
                    CYC_O <= 1'b0;
1151
                    STB_O <= 1'b0;
1152
 
1153
                    interrupt_trap_o <= 8'd24 + { 5'b0, interrupt_mask_o };
1154
 
1155
                    finished_o <= 1'b1;
1156
                    current_state <= S_WAIT;
1157
                end
1158
                else if(ERR_I == 1'b1) begin
1159
                    CYC_O <= 1'b0;
1160
                    STB_O <= 1'b0;
1161
 
1162
                    interrupt_trap_o <= 8'd24; // spurious interrupt
1163
 
1164
                    finished_o <= 1'b1;
1165
                    current_state <= S_WAIT;
1166
                end
1167
            end
1168
 
1169
            S_PC_0: begin
1170
                WE_O <= 1'b0;
1171
                if(supervisor_i == 1'b1)    fc_o <= FC_SUPERVISOR_PROGRAM;
1172
                else                        fc_o <= FC_USER_PROGRAM;
1173
 
1174
                if(pc_i[0] == 1'b1) begin
1175
                    prefetch_ir_valid_32_o <= 1'b1;
1176
                    prefetch_ir_valid_o <= 1'b1;
1177
                    prefetch_ir_valid_80_o <= 1'b1;
1178
 
1179
                    fault_address_state_o <= pc_i;
1180
                    rw_state_o <= 1'b1;
1181
                    fc_state_o <= (supervisor_i == 1'b1) ? FC_SUPERVISOR_PROGRAM : FC_USER_PROGRAM;
1182
                    interrupt_trap_o <= VECTOR_ADDRESS_TRAP;
1183
 
1184
                    jmp_address_trap_o <= 1'b1;
1185
                    current_state <= S_WAIT;
1186
                end
1187
                else begin
1188
                    CYC_O <= 1'b1;
1189
 
1190
                    if(prefetch_ir_valid_32_o == 1'b0)                      ADR_O <= pc_i[31:2];
1191
                    else                                                    ADR_O <= pc_i_plus_6[31:2];
1192
 
1193
                    SEL_O <=    (pc_i[1:0] == 2'b10)?   4'b0011 :
1194
                                                        4'b1111;
1195
                    STB_O <= 1'b1;
1196
 
1197
                    if(prefetch_ir_valid_32_o == 1'b0) begin
1198
                        SGL_O <= 1'b0;
1199
                        BLK_O <= 1'b1;
1200
                        RMW_O <= 1'b0;
1201
                        CTI_O <= CTI_INCR_CYCLE;
1202
                    end
1203
                    else begin
1204
                        SGL_O <= 1'b1;
1205
                        BLK_O <= 1'b0;
1206
                        RMW_O <= 1'b0;
1207
                        CTI_O <= CTI_END_OF_BURST;
1208
                    end
1209
 
1210
                    saved_pc_change <= pc_change_i;
1211
                    prefetch_ir_valid_32_o <= 1'b0;
1212
 
1213
                    current_state <= S_PC_1;
1214
                end
1215
            end
1216
 
1217
            S_PC_1: begin
1218
                if(pc_change_i != 2'b00) saved_pc_change <= pc_change_i;
1219
 
1220
                if(ACK_I == 1'b1) begin
1221
                    if(CTI_O == CTI_INCR_CYCLE) begin
1222
                        //CYC_O <= 1'b1;
1223
                        ADR_O <= pc_i_plus_4[31:2];
1224
                        SEL_O <= 4'b1111;
1225
                        //STB_O <= 1'b1;
1226
                        //WE_O <= 1'b0;
1227
 
1228
                        if(pc_i[1:0] == 2'b10) begin
1229
                            SGL_O <= 1'b0;
1230
                            BLK_O <= 1'b1;
1231
                            RMW_O <= 1'b0;
1232
                            CTI_O <= CTI_INCR_CYCLE;
1233
                        end
1234
                        else begin
1235
                            SGL_O <= 1'b0;
1236
                            BLK_O <= 1'b1;
1237
                            RMW_O <= 1'b0;
1238
                            CTI_O <= CTI_END_OF_BURST;
1239
                        end
1240
 
1241
                        //if(supervisor_i == 1'b1)    fc_o <= FC_SUPERVISOR_PROGRAM;
1242
                        //else                        fc_o <= FC_USER_PROGRAM;
1243
 
1244
                        if(pc_i[1:0] == 2'b10)      prefetch_ir_o <= { DAT_I[15:0], 64'b0 };
1245
                        else                        prefetch_ir_o <= { DAT_I[31:0], 48'b0 };
1246
 
1247
                        current_state <= S_PC_3;
1248
                    end
1249
                    else begin
1250
                        CYC_O <= 1'b0;
1251
                        STB_O <= 1'b0;
1252
 
1253
                        if(saved_pc_change == 2'b10 || saved_pc_change == 2'b11 || pc_change_i == 2'b10 || pc_change_i == 2'b11) begin
1254
                            // load 4 words: [79:16] in 2,3 cycles
1255
                            prefetch_ir_valid_32_o <= 1'b0;
1256
                            prefetch_ir_valid_o <= 1'b0;
1257
                            prefetch_ir_valid_80_o <= 1'b0;
1258
 
1259
                            current_state <= S_PC_0;
1260
                        end
1261
                        else if(saved_pc_change == 2'b01 || pc_change_i == 2'b01) begin
1262
                            // do not load any words
1263
                            prefetch_ir_valid_32_o <= 1'b1;
1264
                            prefetch_ir_valid_o <= 1'b1;
1265
                            prefetch_ir_valid_80_o <= 1'b0;
1266
 
1267
                            prefetch_ir_o <= { prefetch_ir_o[63:32], DAT_I[31:0], 16'b0 };
1268
                            current_state <= S_INIT;
1269
                        end
1270
                        else begin
1271
                            prefetch_ir_valid_32_o <= 1'b1;
1272
                            prefetch_ir_valid_o <= 1'b1;
1273
                            prefetch_ir_valid_80_o <= 1'b1;
1274
 
1275
                            prefetch_ir_o <= { prefetch_ir_o[79:32], DAT_I[31:0] };
1276
                            current_state <= S_INIT;
1277
                        end
1278
                    end
1279
                end
1280
                else if(RTY_I == 1'b1) begin
1281
                    CYC_O <= 1'b0;
1282
                    STB_O <= 1'b0;
1283
 
1284
                    current_state <= S_PC_2;
1285
                end
1286
                else if(ERR_I == 1'b1) begin
1287
                    CYC_O <= 1'b0;
1288
                    STB_O <= 1'b0;
1289
 
1290
                    fault_address_state_o <= { ADR_O, 2'b00 };
1291
                    rw_state_o <= ~WE_O;
1292
                    fc_state_o <= fc_o;
1293
                    interrupt_trap_o <= VECTOR_BUS_TRAP;
1294
 
1295
                    jmp_bus_trap_o <= 1'b1;
1296
                    current_state <= S_WAIT;
1297
                end
1298
            end
1299
            S_PC_2: begin
1300
                CYC_O <= 1'b1;
1301
                STB_O <= 1'b1;
1302
 
1303
                current_state <= S_PC_1;
1304
            end
1305
            S_PC_3: begin
1306
                if(ACK_I == 1'b1) begin
1307
                    if(pc_i[1:0] == 2'b10) begin
1308
                        //CYC_O <= 1'b1;
1309
                        ADR_O <= pc_i_plus_6[31:2];
1310
                        SEL_O <= 4'b1111;
1311
                        //STB_O <= 1'b1;
1312
                        //WE_O <= 1'b0;
1313
 
1314
                        SGL_O <= 1'b0;
1315
                        BLK_O <= 1'b1;
1316
                        RMW_O <= 1'b0;
1317
                        CTI_O <= CTI_END_OF_BURST;
1318
 
1319
                        //if(supervisor_i == 1'b1)    fc_o <= FC_SUPERVISOR_PROGRAM;
1320
                        //else                        fc_o <= FC_USER_PROGRAM;
1321
 
1322
                        prefetch_ir_o <= { prefetch_ir_o[79:64], DAT_I[31:0], 32'b0 };
1323
 
1324
                        current_state <= S_PC_5;
1325
                    end
1326
                    else begin
1327
                        CYC_O <= 1'b0;
1328
                        STB_O <= 1'b0;
1329
 
1330
                        prefetch_ir_o <= { prefetch_ir_o[79:48], DAT_I[31:0], 16'b0 };
1331
 
1332
                        prefetch_ir_valid_32_o <= 1'b1;
1333
                        prefetch_ir_valid_o <= 1'b1;
1334
                        prefetch_ir_valid_80_o <= 1'b0;
1335
                        current_state <= S_INIT;
1336
                    end
1337
                end
1338
                else if(RTY_I == 1'b1) begin
1339
                    CYC_O <= 1'b0;
1340
                    STB_O <= 1'b0;
1341
 
1342
                    current_state <= S_PC_4;
1343
                end
1344
                else if(ERR_I == 1'b1) begin
1345
                    CYC_O <= 1'b0;
1346
                    STB_O <= 1'b0;
1347
 
1348
                    fault_address_state_o <= { ADR_O, 2'b00 };
1349
                    rw_state_o <= ~WE_O;
1350
                    fc_state_o <= fc_o;
1351
                    interrupt_trap_o <= VECTOR_BUS_TRAP;
1352
 
1353
                    jmp_bus_trap_o <= 1'b1;
1354
                    current_state <= S_WAIT;
1355
                end
1356
            end
1357
            S_PC_4: begin
1358
                CYC_O <= 1'b1;
1359
                STB_O <= 1'b1;
1360
 
1361
                current_state <= S_PC_3;
1362
            end
1363
            S_PC_5: begin
1364
                if(ACK_I == 1'b1) begin
1365
                    CYC_O <= 1'b0;
1366
                    STB_O <= 1'b0;
1367
 
1368
                    prefetch_ir_o <= { prefetch_ir_o[79:32], DAT_I[31:0] };
1369
 
1370
                    prefetch_ir_valid_32_o <= 1'b1;
1371
                    prefetch_ir_valid_o <= 1'b1;
1372
                    prefetch_ir_valid_80_o <= 1'b1;
1373
                    current_state <= S_INIT;
1374
                end
1375
                else if(RTY_I == 1'b1) begin
1376
                    CYC_O <= 1'b0;
1377
                    STB_O <= 1'b0;
1378
 
1379
                    current_state <= S_PC_6;
1380
                end
1381
                else if(ERR_I == 1'b1) begin
1382
                    CYC_O <= 1'b0;
1383
                    STB_O <= 1'b0;
1384
 
1385
                    fault_address_state_o <= { ADR_O, 2'b00 };
1386
                    rw_state_o <= ~WE_O;
1387
                    fc_state_o <= fc_o;
1388
                    interrupt_trap_o <= VECTOR_BUS_TRAP;
1389
 
1390
                    jmp_bus_trap_o <= 1'b1;
1391
                    current_state <= S_WAIT;
1392
                end
1393
            end
1394
            S_PC_6: begin
1395
                CYC_O <= 1'b1;
1396
                STB_O <= 1'b1;
1397
 
1398
                current_state <= S_PC_5;
1399
            end
1400
 
1401
            //*******************
1402
            S_READ_1: begin
1403
                if(ACK_I == 1'b1) begin
1404 13 alfik
                    if(address_i[1:0] == 2'b10 && size_i[2] == 1'b1) begin
1405 12 alfik
                        //CYC_O <= 1'b1;
1406
                        ADR_O <= address_i_plus_4[31:2];
1407
                        SEL_O <= 4'b1100;
1408
                        //STB_O <= 1'b1;
1409
                        //WE_O <= 1'b0;
1410
 
1411
                        //SGL_O <= 1'b0;
1412
                        //BLK_O <= 1'b1;
1413
                        //RMW_O <= 1'b0;
1414
                        CTI_O <= CTI_END_OF_BURST;
1415
 
1416
                        //if(supervisor_i == 1'b1)    fc_o <= (address_type_i == 1'b0) ? FC_SUPERVISOR_DATA : FC_SUPERVISOR_PROGRAM;
1417
                        //else                        fc_o <= (address_type_i == 1'b0) ? FC_USER_DATA : FC_USER_PROGRAM;
1418
 
1419
                        data_read_o <= { DAT_I[15:0], 16'b0 };
1420
 
1421
                        current_state <= S_READ_2;
1422
                    end
1423
                    else begin
1424
                        if(read_modify_write_i == 1'b1) begin
1425
                            CYC_O <= 1'b1;
1426
                            STB_O <= 1'b0;
1427
                        end
1428
                        else begin
1429
                            CYC_O <= 1'b0;
1430
                            STB_O <= 1'b0;
1431
                        end
1432
 
1433 13 alfik
                        if(address_i[1:0] == 2'b00 && size_i[2] == 1'b1)             data_read_o <= DAT_I[31:0];
1434
                        else if(address_i[1:0] == 2'b10 && size_i[1] == 1'b1)        data_read_o <= { {16{DAT_I[15]}}, DAT_I[15:0] };
1435
                        else if(address_i[1:0] == 2'b00 && size_i[1] == 1'b1)        data_read_o <= { {16{DAT_I[31]}}, DAT_I[31:16] };
1436
                        else if(address_i[1:0] == 2'b11 && size_i[0] == 1'b1)        data_read_o <= { {24{DAT_I[7]}}, DAT_I[7:0] };
1437
                        else if(address_i[1:0] == 2'b10 && size_i[0] == 1'b1)        data_read_o <= { {24{DAT_I[15]}}, DAT_I[15:8] };
1438
                        else if(address_i[1:0] == 2'b01 && size_i[0] == 1'b1)        data_read_o <= { {24{DAT_I[23]}}, DAT_I[23:16] };
1439
                        else if(address_i[1:0] == 2'b00 && size_i[0] == 1'b1)        data_read_o <= { {24{DAT_I[31]}}, DAT_I[31:24] };
1440 12 alfik
 
1441
                        finished_o <= 1'b1;
1442
                        current_state <= S_WAIT;
1443
                    end
1444
                end
1445
                else if(RTY_I == 1'b1) begin
1446
                    CYC_O <= 1'b0;
1447
                    STB_O <= 1'b0;
1448
 
1449
                    current_state <= S_INIT;
1450
                end
1451
                else if(ERR_I == 1'b1) begin
1452
                    CYC_O <= 1'b0;
1453
                    STB_O <= 1'b0;
1454
 
1455
                    fault_address_state_o <= { ADR_O, 2'b00 };
1456
                    rw_state_o <= ~WE_O;
1457
                    fc_state_o <= fc_o;
1458
                    interrupt_trap_o <= VECTOR_BUS_TRAP;
1459
 
1460
                    jmp_bus_trap_o <= 1'b1;
1461
                    current_state <= S_WAIT;
1462
                end
1463
            end
1464
            S_READ_2: begin
1465
                if(ACK_I == 1'b1) begin
1466
                    CYC_O <= 1'b0;
1467
                    STB_O <= 1'b0;
1468
 
1469
                    data_read_o <= { data_read_o[31:16], DAT_I[31:16] };
1470
 
1471
                    finished_o <= 1'b1;
1472
                    current_state <= S_WAIT;
1473
 
1474
                end
1475
                else if(RTY_I == 1'b1) begin
1476
                    CYC_O <= 1'b0;
1477
                    STB_O <= 1'b0;
1478
 
1479
                    current_state <= S_READ_3;
1480
                end
1481
                else if(ERR_I == 1'b1) begin
1482
                    CYC_O <= 1'b0;
1483
                    STB_O <= 1'b0;
1484
 
1485
                    fault_address_state_o <= { ADR_O, 2'b00 };
1486
                    rw_state_o <= ~WE_O;
1487
                    fc_state_o <= fc_o;
1488
                    interrupt_trap_o <= VECTOR_BUS_TRAP;
1489
 
1490
                    jmp_bus_trap_o <= 1'b1;
1491
                    current_state <= S_WAIT;
1492
                end
1493
 
1494
            end
1495
            S_READ_3: begin
1496
                CYC_O <= 1'b1;
1497
                STB_O <= 1'b1;
1498
 
1499
                current_state <= S_READ_2;
1500
            end
1501
 
1502
 
1503
            S_WAIT: begin
1504
                jmp_address_trap_o <= 1'b0;
1505
                jmp_bus_trap_o <= 1'b0;
1506
 
1507
                if(do_read_i == 1'b0 && do_write_i == 1'b0 && do_interrupt_i == 1'b0 && do_reset_i == 1'b0) begin
1508
                    finished_o <= 1'b0;
1509
                    current_state <= S_INIT;
1510
                end
1511
            end
1512
 
1513
            //**********************
1514
            S_WRITE_1: begin
1515
                if(ACK_I == 1'b1) begin
1516 13 alfik
                    if(address_i[1:0] == 2'b10 && size_i[2] == 1'b1) begin
1517 12 alfik
                        //CYC_O <= 1'b1;
1518
                        ADR_O <= address_i_plus_4[31:2];
1519
                        //STB_O <= 1'b1;
1520
                        //WE_O <= 1'b1;
1521
 
1522
                        DAT_O <= { data_write_i[15:0], 16'b0 };
1523
                        SEL_O <= 4'b1100;
1524
 
1525
                        //SGL_O <= 1'b0;
1526
                        //BLK_O <= 1'b1;
1527
                        //RMW_O <= 1'b0;
1528
                        CTI_O <= CTI_END_OF_BURST;
1529
 
1530
                        //if(supervisor_i == 1'b1)    fc_o <= FC_SUPERVISOR_DATA;
1531
                        //else                        fc_o <= FC_USER_DATA;
1532
 
1533
                        current_state <= S_WRITE_2;
1534
                    end
1535
                    else begin
1536
                        CYC_O <= 1'b0;
1537
                        STB_O <= 1'b0;
1538
 
1539
                        finished_o <= 1'b1;
1540
                        current_state <= S_WAIT;
1541
                    end
1542
                end
1543
                else if(RTY_I == 1'b1) begin
1544
                    CYC_O <= 1'b0;
1545
                    STB_O <= 1'b0;
1546
 
1547
                    current_state <= S_INIT;
1548
                end
1549
                else if(ERR_I == 1'b1) begin
1550
                    CYC_O <= 1'b0;
1551
                    STB_O <= 1'b0;
1552
 
1553
                    fault_address_state_o <= { ADR_O, 2'b00 };
1554
                    rw_state_o <= ~WE_O;
1555
                    fc_state_o <= fc_o;
1556
                    interrupt_trap_o <= VECTOR_BUS_TRAP;
1557
 
1558
                    jmp_bus_trap_o <= 1'b1;
1559
                    current_state <= S_WAIT;
1560
                end
1561
 
1562
            end
1563
            S_WRITE_2: begin
1564
                if(ACK_I == 1'b1) begin
1565
                    CYC_O <= 1'b0;
1566
                    STB_O <= 1'b0;
1567
 
1568
                    finished_o <= 1'b1;
1569
                    current_state <= S_WAIT;
1570
 
1571
                end
1572
                else if(RTY_I == 1'b1) begin
1573
                    CYC_O <= 1'b0;
1574
                    STB_O <= 1'b0;
1575
 
1576
                    current_state <= S_WRITE_3;
1577
                end
1578
                else if(ERR_I == 1'b1) begin
1579
                    CYC_O <= 1'b0;
1580
                    STB_O <= 1'b0;
1581
 
1582
                    fault_address_state_o <= { ADR_O, 2'b00 };
1583
                    rw_state_o <= ~WE_O;
1584
                    fc_state_o <= fc_o;
1585
                    interrupt_trap_o <= VECTOR_BUS_TRAP;
1586
 
1587
                    jmp_bus_trap_o <= 1'b1;
1588
                    current_state <= S_WAIT;
1589
                end
1590
 
1591
            end
1592
            S_WRITE_3: begin
1593
                CYC_O <= 1'b1;
1594
                STB_O <= 1'b1;
1595
 
1596
                current_state <= S_WRITE_2;
1597
            end
1598
 
1599
        endcase
1600
    end
1601
end
1602
 
1603
endmodule
1604
 
1605
/***********************************************************************************************************************
1606
 * Registers
1607
 **********************************************************************************************************************/
1608
 
1609
/*! \brief Microcode controlled registers.
1610
 *
1611
 * Most of the ao68000 IP core registers are located in this module. At every clock cycle the microcode controls what
1612
 * to save into these registers. Some of the more important registers include:
1613
 *  - operand1, operand2 registers are inputs to the ALU,
1614
 *  - address, size, do_read_flag, do_write_flag, do_interrupt_flag registers tell the bus_control module what kind
1615
 *    of bus cycle to perform,
1616
 *  - pc register stores the current program counter,
1617
 *  - ir register stores the current instruction word,
1618
 *  - ea_mod, ea_type registers store the currently selected addressing mode.
1619
 */
1620
module registers(
1621
    input clock,
1622
    input reset_n,
1623
 
1624
    input [31:0] data_read,
1625
    input [79:0] prefetch_ir,
1626
    input prefetch_ir_valid,
1627
    input [31:0] result,
1628
    input [15:0] sr,
1629
    input rw_state,
1630
    input [2:0] fc_state,
1631
    input [31:0] fault_address_state,
1632
    input [7:0] interrupt_trap,
1633
    input [2:0] interrupt_mask,
1634
    input [7:0] decoder_trap,
1635
 
1636
    input [31:0] usp,
1637
    input [31:0] Dn_output,
1638
    input [31:0] An_output,
1639
 
1640
    output [1:0] pc_change,
1641
 
1642
    output reg [2:0] ea_reg,
1643
    input [2:0] ea_reg_control,
1644
 
1645
    output reg [2:0] ea_mod,
1646
    input [3:0] ea_mod_control,
1647
 
1648
    output reg [3:0] ea_type,
1649
    input [3:0] ea_type_control,
1650
 
1651
    // for DIVU/DIVS simulation, register must be not zero
1652
    output reg [31:0] operand1 = 32'hFFFFFFFF,
1653
    input [3:0] operand1_control,
1654
 
1655
    output reg [31:0] operand2 = 32'hFFFFFFFF,
1656
    input [2:0] operand2_control,
1657
 
1658
    output reg [31:0] address,
1659
    output reg address_type,
1660
    input [3:0] address_control,
1661
 
1662 13 alfik
    output reg [2:0] size,
1663 12 alfik
    input [3:0] size_control,
1664
 
1665
    output reg [5:0] movem_modreg,
1666
    input [2:0] movem_modreg_control,
1667
 
1668
    output reg [4:0] movem_loop,
1669
    input [1:0] movem_loop_control,
1670
 
1671
    output reg [15:0] movem_reg,
1672
    input [1:0] movem_reg_control,
1673
 
1674
    output reg [15:0] ir,
1675
    input [1:0] ir_control,
1676
 
1677
    output reg [31:0] pc,
1678
    input [2:0] pc_control,
1679
 
1680
    output reg [7:0] trap,
1681
    input [3:0] trap_control,
1682
 
1683
    output reg [31:0] offset,
1684
    input [1:0] offset_control,
1685
 
1686
    output reg [31:0] index,
1687
    input [1:0] index_control,
1688
 
1689
 
1690
    output reg stop_flag,
1691
    input [1:0] stop_flag_control,
1692
 
1693
    output reg trace_flag,
1694
    input [1:0] trace_flag_control,
1695
 
1696
    output reg group_0_flag,
1697
    input [1:0] group_0_flag_control,
1698
 
1699
    output reg instruction_flag,
1700
    input [1:0] instruction_flag_control,
1701
 
1702
    output reg read_modify_write_flag,
1703
    input [1:0] read_modify_write_flag_control,
1704
 
1705
    output reg do_reset_flag,
1706
    input [1:0] do_reset_flag_control,
1707
 
1708
    output reg do_interrupt_flag,
1709
    input [1:0] do_interrupt_flag_control,
1710
 
1711
    output reg do_read_flag,
1712
    input [1:0] do_read_flag_control,
1713
 
1714
    output reg do_write_flag,
1715
    input [1:0] do_write_flag_control,
1716
 
1717
    output reg do_blocked_flag,
1718
    input [1:0] do_blocked_flag_control,
1719
 
1720
    output reg [31:0] data_write,
1721
    input [1:0] data_write_control,
1722
 
1723
 
1724
    output [3:0] An_address,
1725
    input [1:0] An_address_control,
1726
 
1727
    output [31:0] An_input,
1728
    input [1:0] An_input_control,
1729
 
1730
    output [2:0] Dn_address,
1731
    input Dn_address_control
1732
);
1733
 
1734
reg [31:0] pc_valid;
1735
 
1736
// pc_change connected
1737
always @(posedge clock or negedge reset_n) begin
1738
    if(reset_n == 1'b0) begin
1739
        pc <= 32'd0;
1740
        pc_valid <= 32'd0;
1741
    end
1742
    else begin
1743
        if(pc_control == `PC_FROM_RESULT)                       pc = result;
1744
        else if(pc_control == `PC_INCR_BY_2)                    pc = pc + 32'd2;
1745
        else if(pc_control == `PC_INCR_BY_4)                    pc = pc + 32'd4;
1746 13 alfik
        else if(pc_control == `PC_INCR_BY_SIZE)                 pc = (size[2] == 1'b0) ? pc + 32'd2 : pc + 32'd4;
1747 12 alfik
        else if(pc_control == `PC_FROM_PREFETCH_IR)             pc = prefetch_ir[47:16];
1748
        else if(pc_control == `PC_INCR_BY_2_IN_MAIN_LOOP && prefetch_ir_valid == 1'b1 && decoder_trap == 8'd0 && stop_flag == 1'b0)
1749
                                                                pc = pc + 32'd2;
1750
        if(pc[0] == 1'b0)  pc_valid <= pc;
1751
    end
1752
end
1753
 
1754
assign pc_change =
1755
    (    pc_control == `PC_FROM_RESULT || pc_control == `PC_FROM_PREFETCH_IR
1756
    ) ? 2'b11 :
1757 13 alfik
    (    pc_control == `PC_INCR_BY_4 || (pc_control == `PC_INCR_BY_SIZE && size[2] == 1'b1)
1758 12 alfik
    ) ? 2'b10 :
1759 13 alfik
    (    pc_control == `PC_INCR_BY_2 || (pc_control == `PC_INCR_BY_SIZE && size[2] == 1'b0) ||
1760 12 alfik
        (pc_control == `PC_INCR_BY_2_IN_MAIN_LOOP && prefetch_ir_valid == 1'b1 && decoder_trap == 8'd0 && stop_flag == 1'b0)
1761
    ) ? 2'b01 :
1762
    2'b00;
1763
 
1764
always @(posedge clock or negedge reset_n) begin
1765 13 alfik
    if(reset_n == 1'b0) begin
1766
        size <= 2'b00;
1767
    end
1768 15 alfik
    else if(size_control != `SIZE_IDLE) begin
1769 13 alfik
        // BYTE
1770
        size[0] <= (size_control == `SIZE_BYTE)
1771
                | ((size_control == `SIZE_3) && (ir[7:6] == 2'b00))
1772
                | ((size_control == `SIZE_4) && (ir[13:12] == 2'b01))
1773
                | ((size_control == `SIZE_6) && (ir[5:3] != 3'b000));
1774
        // WORD
1775
        size[1] <= (size_control == `SIZE_WORD)
1776
                | ((size_control == `SIZE_1) && (ir[7:6] == 2'b00))
1777
                | ((size_control == `SIZE_1_PLUS) && (ir[7:6] == 2'b10))
1778
                | ((size_control == `SIZE_2) && (ir[6] == 1'b0))
1779
                | ((size_control == `SIZE_3) && (ir[7:6] == 2'b01))
1780
                | ((size_control == `SIZE_4) && (ir[13:12] == 2'b11))
1781
                | ((size_control == `SIZE_5) && (ir[8] == 1'b0));
1782
        // LONG
1783
        size[2] <= (size_control == `SIZE_LONG)
1784
                | ((size_control == `SIZE_1) && (ir[7:6] != 2'b00))
1785
                | ((size_control == `SIZE_1_PLUS) && (ir[7:6] != 2'b10))
1786
                | ((size_control == `SIZE_2) && (ir[6] == 1'b1))
1787
                | ((size_control == `SIZE_3) && (ir[7] == 1'b1))
1788
                | ((size_control == `SIZE_4) && (ir[12] == 1'b0))
1789
                | ((size_control == `SIZE_5) && (ir[8] == 1'b1))
1790
                | ((size_control == `SIZE_6) && (ir[5:3] == 3'b000));
1791
    end
1792
end
1793 12 alfik
 
1794
always @(posedge clock or negedge reset_n) begin
1795
    if(reset_n == 1'b0)                                         ea_reg <= 3'b000;
1796
    else if(ea_reg_control == `EA_REG_IR_2_0)                   ea_reg <= ir[2:0];
1797
    else if(ea_reg_control == `EA_REG_IR_11_9)                  ea_reg <= ir[11:9];
1798
    else if(ea_reg_control == `EA_REG_MOVEM_REG_2_0)            ea_reg <= movem_modreg[2:0];
1799
    else if(ea_reg_control == `EA_REG_3b111)                    ea_reg <= 3'b111;
1800
    else if(ea_reg_control == `EA_REG_3b100)                    ea_reg <= 3'b100;
1801
end
1802
 
1803
always @(posedge clock or negedge reset_n) begin
1804
    if(reset_n == 1'b0)                                         ea_mod <= 3'b000;
1805
    else if(ea_mod_control == `EA_MOD_IR_5_3)                   ea_mod <= ir[5:3];
1806
    else if(ea_mod_control == `EA_MOD_MOVEM_MOD_5_3)            ea_mod <= movem_modreg[5:3];
1807
    else if(ea_mod_control == `EA_MOD_IR_8_6)                   ea_mod <= ir[8:6];
1808
    else if(ea_mod_control == `EA_MOD_PREDEC)                   ea_mod <= 3'b100;
1809
    else if(ea_mod_control == `EA_MOD_3b111)                    ea_mod <= 3'b111;
1810
    else if(ea_mod_control == `EA_MOD_DN_PREDEC)                ea_mod <= (ir[3] == 1'b0) ? /* Dn */ 3'b000 : /* -(An) */ 3'b100;
1811
    else if(ea_mod_control == `EA_MOD_DN_AN_EXG)                ea_mod <= (ir[7:3] == 5'b01000 || ir[7:3] == 5'b10001) ? /* Dn */ 3'b000 : /* An */ 3'b001;
1812
    else if(ea_mod_control == `EA_MOD_POSTINC)                  ea_mod <= 3'b011;
1813
    else if(ea_mod_control == `EA_MOD_AN)                       ea_mod <= 3'b001;
1814
    else if(ea_mod_control == `EA_MOD_DN)                       ea_mod <= 3'b000;
1815
    else if(ea_mod_control == `EA_MOD_INDIRECTOFFSET)           ea_mod <= 3'b101;
1816
end
1817
 
1818
always @(posedge clock or negedge reset_n) begin
1819
    if(reset_n == 1'b0)                                         ea_type <= `EA_TYPE_IDLE;
1820
    else if(ea_type_control == `EA_TYPE_ALL)                    ea_type <= `EA_TYPE_ALL;
1821
    else if(ea_type_control == `EA_TYPE_CONTROL_POSTINC)        ea_type <= `EA_TYPE_CONTROL_POSTINC;
1822
    else if(ea_type_control == `EA_TYPE_CONTROLALTER_PREDEC)    ea_type <= `EA_TYPE_CONTROLALTER_PREDEC;
1823
    else if(ea_type_control == `EA_TYPE_CONTROL)                ea_type <= `EA_TYPE_CONTROL;
1824
    else if(ea_type_control == `EA_TYPE_DATAALTER)              ea_type <= `EA_TYPE_DATAALTER;
1825
    else if(ea_type_control == `EA_TYPE_DN_AN)                  ea_type <= `EA_TYPE_DN_AN;
1826
    else if(ea_type_control == `EA_TYPE_MEMORYALTER)            ea_type <= `EA_TYPE_MEMORYALTER;
1827
    else if(ea_type_control == `EA_TYPE_DATA)                   ea_type <= `EA_TYPE_DATA;
1828
end
1829
 
1830
always @(posedge clock or negedge reset_n) begin
1831
    if(reset_n == 1'b0)                                         operand1 <= 32'hFFFFFFFF;
1832
    else if(operand1_control == `OP1_FROM_OP2)                  operand1 <= operand2;
1833
    else if(operand1_control == `OP1_FROM_ADDRESS)              operand1 <= address;
1834
    else if(operand1_control == `OP1_FROM_DATA)                 operand1 <=
1835 13 alfik
                                                                    (size[0] == 1'b1) ? { {24{data_read[7]}}, data_read[7:0] } :
1836
                                                                    (size[1] == 1'b1) ? { {16{data_read[15]}}, data_read[15:0] } :
1837 12 alfik
                                                                    data_read[31:0];
1838
    else if(operand1_control == `OP1_FROM_IMMEDIATE)            operand1 <=
1839 13 alfik
                                                                    (size[0] == 1'b1) ? { {24{prefetch_ir[71]}}, prefetch_ir[71:64] } :
1840
                                                                    (size[1] == 1'b1) ? { {16{prefetch_ir[79]}}, prefetch_ir[79:64] } :
1841 12 alfik
                                                                    prefetch_ir[79:48];
1842
    else if(operand1_control == `OP1_FROM_RESULT)               operand1 <= result;
1843
    else if(operand1_control == `OP1_MOVEQ)                     operand1 <= { {24{ir[7]}}, ir[7:0] };
1844
    else if(operand1_control == `OP1_FROM_PC)                   operand1 <= pc_valid;
1845
    else if(operand1_control == `OP1_LOAD_ZEROS)                operand1 <= 32'b0;
1846
    else if(operand1_control == `OP1_LOAD_ONES)                 operand1 <= 32'hFFFFFFFF;
1847
    else if(operand1_control == `OP1_FROM_SR)                   operand1 <= { 16'b0, sr[15], 1'b0, sr[13], 2'b0, sr[10:8], 3'b0, sr[4:0] };
1848
    else if(operand1_control == `OP1_FROM_USP)                  operand1 <= usp;
1849
    else if(operand1_control == `OP1_FROM_AN)                   operand1 <=
1850 13 alfik
                                                                    (size[1] == 1'b1) ? { {16{An_output[15]}}, An_output[15:0] } :
1851 12 alfik
                                                                    An_output[31:0];
1852
    else if(operand1_control == `OP1_FROM_DN)                   operand1 <=
1853 13 alfik
                                                                    (size[0] == 1'b1) ? { {24{Dn_output[7]}}, Dn_output[7:0] } :
1854
                                                                    (size[1] == 1'b1) ? { {16{Dn_output[15]}}, Dn_output[15:0] } :
1855 12 alfik
                                                                    Dn_output[31:0];
1856
    else if(operand1_control == `OP1_FROM_IR)                   operand1 <= { 16'b0, ir[15:0] };
1857
    else if(operand1_control == `OP1_FROM_FAULT_ADDRESS)        operand1 <= fault_address_state;
1858
end
1859
 
1860
always @(posedge clock or negedge reset_n) begin
1861
    if(reset_n == 1'b0)                                         operand2 <= 32'hFFFFFFFF;
1862
    else if(operand2_control == `OP2_FROM_OP1)                  operand2 <= operand1;
1863
    else if(operand2_control == `OP2_LOAD_1)                    operand2 <= 32'd1;
1864
    else if(operand2_control == `OP2_LOAD_COUNT)                operand2 <=
1865
                                                                    (ir[5] == 1'b0) ? ( (ir[11:9] == 3'b000) ? 32'b1000 : { 29'b0, ir[11:9] } ) :
1866
                                                                    { 26'b0, operand2[5:0] };
1867
    else if(operand2_control == `OP2_ADDQ_SUBQ)                 operand2 <= (ir[11:9] == 3'b000) ? 32'b1000 : { 29'b0, ir[11:9] };
1868
    else if(operand2_control == `OP2_MOVE_OFFSET)               operand2 <= (ir[7:0] == 8'b0) ? operand2[31:0] : { {24{ir[7]}}, ir[7:0] };
1869
    else if(operand2_control == `OP2_MOVE_ADDRESS_BUS_INFO)     operand2 <= { 16'b0, 11'b0, rw_state, instruction_flag, fc_state};
1870
    else if(operand2_control == `OP2_DECR_BY_1)                 operand2 <= operand2 - 32'b1;
1871
end
1872
 
1873
always @(posedge clock or negedge reset_n) begin
1874
    if(reset_n == 1'b0)                                         address <= 32'b0;
1875 13 alfik
    else if(address_control == `ADDRESS_INCR_BY_SIZE)           address <= ((size[0]) && ea_reg == 3'b111) ? address + 32'd2 : address + {29'd0,size};
1876
    else if(address_control == `ADDRESS_DECR_BY_SIZE)           address <= ((size[0]) && ea_reg == 3'b111) ? address - 32'd2 : address - {29'd0,size};
1877
    else if(address_control == `ADDRESS_INCR_BY_2)              address <= address + 32'd2;
1878
    else if(address_control == `ADDRESS_FROM_AN_OUTPUT)         address <= An_output;
1879
    else if(address_control == `ADDRESS_FROM_BASE_INDEX_OFFSET) address <= address + index + offset;
1880
    else if(address_control == `ADDRESS_FROM_IMM_16)            address <= { {16{prefetch_ir[79]}}, prefetch_ir[79:64] };
1881
    else if(address_control == `ADDRESS_FROM_IMM_32)            address <= prefetch_ir[79:48];
1882
    else if(address_control == `ADDRESS_FROM_PC_INDEX_OFFSET)   address <= pc_valid + index + offset;
1883
    else if(address_control == `ADDRESS_FROM_TRAP)              address <= {22'b0, trap[7:0], 2'b0};
1884
end
1885 12 alfik
 
1886
always @(posedge clock or negedge reset_n) begin
1887
    if(reset_n == 1'b0)                                         address_type <= 1'b0;
1888
    else if(address_control == `ADDRESS_FROM_PC_INDEX_OFFSET)   address_type <= 1'b1;
1889
    else if(address_control != `ADDRESS_IDLE)                   address_type <= 1'b0;
1890
end
1891
 
1892
always @(posedge clock or negedge reset_n) begin
1893
    if(reset_n == 1'b0)                                         movem_modreg <= 6'b0;
1894
    else if(movem_modreg_control == `MOVEM_MODREG_LOAD_0)       movem_modreg <= 6'b0;
1895
    else if(movem_modreg_control == `MOVEM_MODREG_LOAD_6b001111)movem_modreg <= 6'b001111;
1896
    else if(movem_modreg_control == `MOVEM_MODREG_INCR_BY_1)    movem_modreg <= movem_modreg + 6'd1;
1897
    else if(movem_modreg_control == `MOVEM_MODREG_DECR_BY_1)    movem_modreg <= movem_modreg - 6'd1;
1898
end
1899
 
1900
always @(posedge clock or negedge reset_n) begin
1901
    if(reset_n == 1'b0)                                         movem_loop <= 5'b0;
1902
    else if(movem_loop_control == `MOVEM_LOOP_LOAD_0)           movem_loop <= 5'b0;
1903
    else if(movem_loop_control == `MOVEM_LOOP_INCR_BY_1)        movem_loop <= movem_loop + 5'd1;
1904
end
1905
 
1906
always @(posedge clock or negedge reset_n) begin
1907
    if(reset_n == 1'b0)                                         movem_reg <= 16'b0;
1908
    else if(movem_reg_control == `MOVEM_REG_FROM_OP1)           movem_reg <= operand1[15:0];
1909
    else if(movem_reg_control == `MOVEM_REG_SHIFT_RIGHT)        movem_reg <= { 1'b0, movem_reg[15:1] };
1910
end
1911
 
1912
always @(posedge clock or negedge reset_n) begin
1913
    if(reset_n == 1'b0)                                         ir <= 16'b0;
1914
    else if(ir_control == `IR_LOAD_WHEN_PREFETCH_VALID && prefetch_ir_valid == 1'b1 && stop_flag == 1'b0)
1915
                                                                ir <= prefetch_ir[79:64];
1916
end
1917
 
1918
always @(posedge clock or negedge reset_n) begin
1919
    if(reset_n == 1'b0)                                         trap <= 8'd0;
1920
    else if(trap_control == `TRAP_ILLEGAL_INSTR)                trap <= 8'd4;
1921
    else if(trap_control == `TRAP_DIV_BY_ZERO)                  trap <= 8'd5;
1922
    else if(trap_control == `TRAP_CHK)                          trap <= 8'd6;
1923
    else if(trap_control == `TRAP_TRAPV)                        trap <= 8'd7;
1924
    else if(trap_control == `TRAP_PRIVIL_VIOLAT)                trap <= 8'd8;
1925
    else if(trap_control == `TRAP_TRACE)                        trap <= 8'd9;
1926
    else if(trap_control == `TRAP_TRAP)                         trap <= { 4'b0010, ir[3:0] };
1927
    else if(trap_control == `TRAP_FROM_DECODER)                 trap <= decoder_trap;
1928
    else if(trap_control == `TRAP_FROM_INTERRUPT)               trap <= interrupt_trap;
1929
end
1930
 
1931
always @(posedge clock or negedge reset_n) begin
1932
    if(reset_n == 1'b0)                                         offset <= 32'd0;
1933
    else if(offset_control == `OFFSET_IMM_8)                    offset <= { {24{prefetch_ir[71]}}, prefetch_ir[71:64] };
1934
    else if(offset_control == `OFFSET_IMM_16)                   offset <= { {16{prefetch_ir[79]}}, prefetch_ir[79:64] };
1935
end
1936
 
1937
always @(posedge clock or negedge reset_n) begin
1938
    if(reset_n == 1'b0)                                         index <= 32'd0;
1939
    else if(index_control == `INDEX_0)                          index <= 32'd0;
1940
    else if(index_control == `INDEX_LOAD_EXTENDED)              index <=
1941
                                                                    (prefetch_ir[79] == 1'b0) ?
1942
                                                                    (     (prefetch_ir[75] == 1'b0)  ?
1943
                                                                            { {16{Dn_output[15]}}, Dn_output[15:0] } : Dn_output[31:0]
1944
                                                                    ) :
1945
                                                                    (     (prefetch_ir[75] == 1'b0) ?
1946
                                                                            { {16{An_output[15]}}, An_output[15:0] } : An_output[31:0]
1947
                                                                    );
1948
end
1949
 
1950
always @(posedge clock or negedge reset_n) begin
1951
    if(reset_n == 1'b0)                                         stop_flag <= 1'b0;
1952
    else if(stop_flag_control == `STOP_FLAG_SET)                stop_flag <= 1'b1;
1953
    else if(stop_flag_control == `STOP_FLAG_CLEAR)              stop_flag <= 1'b0;
1954
end
1955
 
1956
always @(posedge clock or negedge reset_n) begin
1957
    if(reset_n == 1'b0)                                         trace_flag <= 1'b0;
1958
    else if(trace_flag_control == `TRACE_FLAG_COPY_WHEN_NO_STOP && prefetch_ir_valid == 1'b1 && decoder_trap == 8'd0 && stop_flag == 1'b0)
1959
                                                                trace_flag <= sr[15];
1960
end
1961
 
1962
always @(posedge clock or negedge reset_n) begin
1963
    if(reset_n == 1'b0)                                         group_0_flag <= 1'b0;
1964
    else if(group_0_flag_control == `GROUP_0_FLAG_SET)          group_0_flag <= 1'b1;
1965
    else if(group_0_flag_control == `GROUP_0_FLAG_CLEAR_WHEN_VALID_PREFETCH && prefetch_ir_valid == 1'b1 && stop_flag == 1'b0)
1966
                                                                group_0_flag <= 1'b0;
1967
end
1968
 
1969
always @(posedge clock or negedge reset_n) begin
1970
    if(reset_n == 1'b0)                                         instruction_flag <= 1'b0;
1971
    else if(instruction_flag_control == `INSTRUCTION_FLAG_SET)  instruction_flag <= 1'b1;
1972
    else if(instruction_flag_control == `INSTRUCTION_FLAG_CLEAR_IN_MAIN_LOOP && prefetch_ir_valid == 1'b1 && decoder_trap == 8'd0 && stop_flag == 1'b0)
1973
                                                                instruction_flag <= 1'b0;
1974
end
1975
 
1976
always @(posedge clock or negedge reset_n) begin
1977
    if(reset_n == 1'b0)                                                         read_modify_write_flag <= 1'b0;
1978
    else if(read_modify_write_flag_control == `READ_MODIFY_WRITE_FLAG_SET)      read_modify_write_flag <= 1'b1;
1979
    else if(read_modify_write_flag_control == `READ_MODIFY_WRITE_FLAG_CLEAR)    read_modify_write_flag <= 1'b0;
1980
end
1981
 
1982
always @(posedge clock or negedge reset_n) begin
1983
    if(reset_n == 1'b0)                                         do_reset_flag <= 1'b0;
1984
    else if(do_reset_flag_control == `DO_RESET_FLAG_SET)        do_reset_flag <= 1'b1;
1985
    else if(do_reset_flag_control == `DO_RESET_FLAG_CLEAR)      do_reset_flag <= 1'b0;
1986
end
1987
 
1988
always @(posedge clock or negedge reset_n) begin
1989
    if(reset_n == 1'b0)                                                         do_interrupt_flag <= 1'b0;
1990
    else if(do_interrupt_flag_control == `DO_INTERRUPT_FLAG_SET_IF_ACTIVE)      do_interrupt_flag <= (interrupt_mask != 3'b000) ? 1'b1 : 1'b0;
1991
    else if(do_interrupt_flag_control == `DO_INTERRUPT_FLAG_CLEAR)              do_interrupt_flag <= 1'b0;
1992
end
1993
 
1994
always @(posedge clock or negedge reset_n) begin
1995
    if(reset_n == 1'b0)                                         do_read_flag <= 1'b0;
1996
    else if(do_read_flag_control == `DO_READ_FLAG_SET)          do_read_flag <= 1'b1;
1997
    else if(do_read_flag_control == `DO_READ_FLAG_CLEAR)        do_read_flag <= 1'b0;
1998
end
1999
 
2000
always @(posedge clock or negedge reset_n) begin
2001
    if(reset_n == 1'b0)                                         do_write_flag <= 1'b0;
2002
    else if(do_write_flag_control == `DO_WRITE_FLAG_SET)        do_write_flag <= 1'b1;
2003
    else if(do_write_flag_control == `DO_WRITE_FLAG_CLEAR)      do_write_flag <= 1'b0;
2004
end
2005
 
2006
always @(posedge clock or negedge reset_n) begin
2007
    if(reset_n == 1'b0)                                         do_blocked_flag <= 1'b0;
2008
    else if(do_blocked_flag_control == `DO_BLOCKED_FLAG_SET)    do_blocked_flag <= 1'b1;
2009
end
2010
 
2011
always @(posedge clock or negedge reset_n) begin
2012
    if(reset_n == 1'b0)                                         data_write <= 32'd0;
2013
    else if(data_write_control == `DATA_WRITE_FROM_RESULT)      data_write <= result;
2014
end
2015
 
2016
assign An_address =
2017
    (An_address_control == `AN_ADDRESS_FROM_EXTENDED) ? { sr[13], prefetch_ir[78:76] } :
2018
    (An_address_control == `AN_ADDRESS_USP) ?           4'b0111 :
2019
    (An_address_control == `AN_ADDRESS_SSP) ?           4'b1111 :
2020
    { sr[13], ea_reg };
2021
 
2022
assign An_input =
2023
    (An_input_control == `AN_INPUT_FROM_ADDRESS) ?      address :
2024
    (An_input_control == `AN_INPUT_FROM_PREFETCH_IR) ?  prefetch_ir[79:48] :
2025
    result;
2026
 
2027
assign Dn_address = (Dn_address_control == `DN_ADDRESS_FROM_EXTENDED) ? prefetch_ir[78:76] : ea_reg;
2028
 
2029
endmodule
2030
 
2031
/***********************************************************************************************************************
2032
 * Memory registers
2033
 **********************************************************************************************************************/
2034
 
2035
/*! \brief Contains the microcode ROM and D0-D7, A0-A7 registers.
2036
 *
2037
 * The memory_registers module contains:
2038
 *  - data and address registers (D0-D7, A0-A7) implemented as an on-chip RAM.
2039
 *  - the microcode implemented as an on-chip ROM.
2040
 *
2041
 * Currently this module contains <em>altsyncram</em> instantiations
2042
 * from Altera Megafunction/LPM library.
2043
 */
2044
module memory_registers(
2045
    input clock,
2046
    input reset_n,
2047
 
2048
    // 0000,0001,0010,0011,0100,0101,0110: A0-A6, 0111: USP, 1111: SSP
2049
    input [3:0] An_address,
2050
    input [31:0] An_input,
2051
    input An_write_enable,
2052
    output [31:0] An_output,
2053
 
2054
    output reg [31:0] usp,
2055
 
2056
    input [2:0] Dn_address,
2057
    input [31:0] Dn_input,
2058
    input Dn_write_enable,
2059 13 alfik
    // 001: byte, 010: word, 100: long
2060
    input [2:0] Dn_size,
2061 12 alfik
    output [31:0] Dn_output,
2062
 
2063
    input [8:0] micro_pc,
2064
    output [87:0] micro_data
2065
);
2066
 
2067
wire An_ram_write_enable    = (An_address == 4'b0111) ? 1'b0 : An_write_enable;
2068
 
2069
wire [31:0] An_ram_output;
2070
assign An_output            = (An_address == 4'b0111) ? usp : An_ram_output;
2071
 
2072 13 alfik
wire [3:0] dn_byteena       = (Dn_size[0] == 1'b1) ? 4'b0001 :
2073
                              (Dn_size[1] == 1'b1) ? 4'b0011 :
2074
                              (Dn_size[2] == 1'b1) ? 4'b1111 :
2075 12 alfik
                              4'b0000;
2076
 
2077
always @(posedge clock or negedge reset_n) begin
2078
    if(reset_n == 1'b0)                                 usp <= 32'd0;
2079
    else if(An_address == 4'b0111 && An_write_enable)   usp <= An_input;
2080
end
2081
 
2082
// Register set An implemented as RAM.
2083
altsyncram an_ram_inst(
2084
    .clock0     (clock),
2085
 
2086
    .address_a  (An_address[2:0]),
2087
    .byteena_a  (4'b1111),
2088
    .wren_a     (An_ram_write_enable),
2089
    .data_a     (An_input),
2090
    .q_a        (An_ram_output)
2091
);
2092
defparam
2093
    an_ram_inst.operation_mode      = "SINGLE_PORT",
2094
    an_ram_inst.width_a             = 32,
2095
    an_ram_inst.widthad_a           = 3,
2096
    an_ram_inst.width_byteena_a     = 4;
2097
 
2098
// Register set Dn implemented as RAM.
2099
altsyncram dn_ram_inst(
2100
    .clock0     (clock),
2101
 
2102
    .address_a  (Dn_address),
2103
    .byteena_a  (dn_byteena),
2104
    .wren_a     (Dn_write_enable),
2105
    .data_a     (Dn_input),
2106
    .q_a        (Dn_output)
2107
);
2108
defparam
2109
    dn_ram_inst.operation_mode      = "SINGLE_PORT",
2110
    dn_ram_inst.width_a             = 32,
2111
    dn_ram_inst.widthad_a           = 3,
2112
    dn_ram_inst.width_byteena_a     = 4;
2113
 
2114
// Microcode ROM
2115
altsyncram micro_rom_inst(
2116
    .clock0     (clock),
2117
 
2118
    .address_a  (micro_pc),
2119
    .q_a        (micro_data)
2120
);
2121
defparam
2122
    micro_rom_inst.operation_mode   = "ROM",
2123
    micro_rom_inst.width_a          = 88,
2124
    micro_rom_inst.widthad_a        = 9,
2125
    micro_rom_inst.init_file        = "ao68000_microcode.mif";
2126
 
2127
endmodule
2128
 
2129
/***********************************************************************************************************************
2130
 * Instruction decoder
2131
 **********************************************************************************************************************/
2132
 
2133
/*! \brief Decode instruction and addressing mode.
2134
 *
2135
 * The decoder is an instruction and addressing mode decoder. For instructions it takes as input the ir register
2136
 * from the registers module. The output of the decoder, in this case, is a microcode address of the first microcode
2137
 * word that performs the instruction.
2138
 *
2139
 * In case of addressing mode decoding, the output is the address of the first microcode word that performs the operand
2140
 * loading or saving. This address is obtained from the currently selected addressing mode saved in the ea_mod
2141
 * and ea_type registers in the registers module.
2142
 */
2143
module decoder(
2144
    input clock,
2145
    input reset_n,
2146
 
2147
    input supervisor,
2148
    input [15:0] ir,
2149
 
2150
    // zero: no trap
2151
    output [7:0] decoder_trap,
2152
    output [8:0] decoder_micropc,
2153
 
2154
    output [8:0] save_ea,
2155
    output [8:0] perform_ea_write,
2156
    output [8:0] perform_ea_read,
2157
    output [8:0] load_ea,
2158
 
2159
    input [3:0] ea_type,
2160
    input [2:0] ea_mod,
2161
    input [2:0] ea_reg
2162
);
2163
 
2164
parameter [7:0]
2165
    NO_TRAP                             = 8'd0,
2166
    ILLEGAL_INSTRUCTION_TRAP            = 8'd4,
2167
    PRIVILEGE_VIOLATION_TRAP            = 8'd8,
2168
    ILLEGAL_1010_INSTRUCTION_TRAP       = 8'd10,
2169
    ILLEGAL_1111_INSTRUCTION_TRAP       = 8'd11;
2170
 
2171
parameter [8:0]
2172
    UNUSED_MICROPC                      = 9'd0;
2173
 
2174
assign { decoder_trap, decoder_micropc } =
2175
    (reset_n == 1'b0) ? { NO_TRAP, UNUSED_MICROPC } :
2176
 
2177
    // Privilege violation and illegal instruction
2178
 
2179
    // ANDI to SR,EORI to SR,ORI to SR,RESET,STOP,RTE,MOVE TO SR,MOVE USP TO USP,MOVE USP TO An privileged instructions
2180
    ( ( ir[15:0] == 16'b0000_0010_01_111_100 ||
2181
          ir[15:0] == 16'b0000_1010_01_111_100 ||
2182
          ir[15:0] == 16'b0000_0000_01_111_100 ||
2183
          ir[15:0] == 16'b0100_1110_0111_0000 ||
2184
          ir[15:0] == 16'b0100_1110_0111_0010 ||
2185
          ir[15:0] == 16'b0100_1110_0111_0011 ||
2186
         (ir[15:6] == 10'b0100_0110_11 && ir[5:3] != 3'b001 && ir[5:0] != 6'b111_101 && ir[5:0] != 6'b111_110 && ir[5:0] != 6'b111_111) ||
2187
          ir[15:3] == 13'b0100_1110_0110_0 ||
2188
          ir[15:3] == 13'b0100_1110_0110_1 ) && supervisor == 1'b0 ) ? { PRIVILEGE_VIOLATION_TRAP, UNUSED_MICROPC } :
2189
    // ILLEGAL, illegal instruction
2190
    ( ir[15:0] == 16'b0100_1010_11_111100 ) ? { ILLEGAL_INSTRUCTION_TRAP, UNUSED_MICROPC } :
2191
    // 1010 illegal instruction
2192
    ( ir[15:12] == 4'b1010 ) ? { ILLEGAL_1010_INSTRUCTION_TRAP, UNUSED_MICROPC } :
2193
    // 1111 illegal instruction
2194
    ( ir[15:12] == 4'b1111 ) ? { ILLEGAL_1111_INSTRUCTION_TRAP, UNUSED_MICROPC } :
2195
 
2196
    // instruction decoding
2197
 
2198
    // ANDI,EORI,ORI,ADDI,SUBI
2199
    ( ir[15:12] == 4'b0000 && ir[11:9] != 3'b100 && ir[11:9] != 3'b110 && ir[11:9] != 3'b111 && ir[8] == 1'b0 &&
2200
        (ir[7:6] == 2'b00 || ir[7:6] == 2'b01 || ir[7:6] == 2'b10) && ir[5:3] != 3'b001 &&
2201
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001)) &&
2202
        ir[15:0] != 16'b0000_000_0_00_111100 && ir[15:0] != 16'b0000_000_0_01_111100 &&
2203
        ir[15:0] != 16'b0000_001_0_00_111100 && ir[15:0] != 16'b0000_001_0_01_111100 &&
2204
        ir[15:0] != 16'b0000_101_0_00_111100 && ir[15:0] != 16'b0000_101_0_01_111100 ) ? { NO_TRAP, `MICROPC_ANDI_EORI_ORI_ADDI_SUBI } :
2205
    // ORI to CCR,ORI to SR,ANDI to CCR,ANDI to SR,EORI to CCR,EORI to SR
2206
    ( ir[15:0] == 16'b0000_000_0_00_111100 || ir[15:0] == 16'b0000_000_0_01_111100 ||
2207
        ir[15:0] == 16'b0000_001_0_00_111100 || ir[15:0] == 16'b0000_001_0_01_111100 ||
2208
        ir[15:0] == 16'b0000_101_0_00_111100 || ir[15:0] == 16'b0000_101_0_01_111100 ) ?
2209
        { NO_TRAP, `MICROPC_ORI_to_CCR_ORI_to_SR_ANDI_to_CCR_ANDI_to_SR_EORI_to_CCR_EORI_to_SR } :
2210
    // BTST register
2211
    ( ir[15:12] == 4'b0000 && ir[8:6] == 3'b100 && ir[5:3] != 3'b001 &&
2212
        (ir[5:3] != 3'b111 ||
2213
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2214
    ) ? { NO_TRAP, `MICROPC_BTST_register } :
2215
    // MOVEP memory to register
2216
    ( ir[15:12] == 4'b0000 && ir[8] == 1'b1 && ir[5:3] == 3'b001 && ( ir[7:6] == 2'b00 || ir[7:6] == 2'b01 ) ) ?
2217
        { NO_TRAP, `MICROPC_MOVEP_memory_to_register } :
2218
    // MOVEP register to memory
2219
    ( ir[15:12] == 4'b0000 && ir[8] == 1'b1 && ir[5:3] == 3'b001 && ( ir[7:6] == 2'b10 || ir[7:6] == 2'b11 ) ) ?
2220
        { NO_TRAP, `MICROPC_MOVEP_register_to_memory } :
2221
    // BCHG,BCLR,BSET register
2222
    ( ir[15:12] == 4'b0000 && ir[8] == 1'b1 && ir[5:3] != 3'b001 && ir[8:6] != 3'b100 &&
2223
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2224
    ) ?  { NO_TRAP, `MICROPC_BCHG_BCLR_BSET_register } :
2225
    // BTST immediate
2226
    ( ir[15:12] == 4'b0000 && ir[11:8] == 4'b1000 && ir[7:6] == 2'b00 && ir[5:3] != 3'b001 &&
2227
        (ir[5:3] != 3'b111 ||
2228
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011))
2229
    ) ? { NO_TRAP, `MICROPC_BTST_immediate } :
2230
    // BCHG,BCLR,BSET immediate
2231
    ( ir[15:12] == 4'b0000 && ir[11:8] == 4'b1000 && ir[7:6] != 2'b00 && ir[5:3] != 3'b001 &&
2232
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2233
    ) ? { NO_TRAP, `MICROPC_BCHG_BCLR_BSET_immediate } :
2234
    // CMPI
2235
    ( ir[15:12] == 4'b0000 && ir[8] == 1'b0 && ir[11:9] == 3'b110 && ir[7:6] != 2'b11 && ir[5:3] != 3'b001 &&
2236
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2237
    ) ? { NO_TRAP, `MICROPC_CMPI } :
2238
    // MOVE
2239
    ( ir[15:14] == 2'b00 && ir[13:12] != 2'b00 && ir[8:6] != 3'b001 &&
2240
        (ir[8:6] != 3'b111 || (ir[11:6] == 6'b000_111 || ir[11:6] == 6'b001_111)) &&
2241
        (ir[13:12] != 2'b01 || ir[5:3] != 3'b001) &&
2242
        (ir[5:3] != 3'b111 ||
2243
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2244
    ) ? { NO_TRAP, `MICROPC_MOVE } :
2245
    // MOVEA
2246
    ( ir[15:14] == 2'b00 && (ir[13:12] == 2'b11 || ir[13:12] == 2'b10) && ir[8:6] == 3'b001 &&
2247
        (ir[5:3] != 3'b111 ||
2248
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2249
    ) ? { NO_TRAP, `MICROPC_MOVEA } :
2250
    // NEGX,CLR,NEG,NOT,NBCD
2251
    (    ir[15:12] == 4'b0100 && ir[5:3] != 3'b001 && (ir[5:3] != 3'b111 || ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001) &&
2252
            (    (ir[11:8] == 4'b0000 && ir[7:6] != 2'b11) || (ir[11:8] == 4'b0010 && ir[7:6] != 2'b11) ||
2253
                (ir[11:8] == 4'b0100 && ir[7:6] != 2'b11) || (ir[11:8] == 4'b0110 && ir[7:6] != 2'b11) ||
2254
                (ir[11:6] == 6'b1000_00)
2255
            )
2256
    ) ? { NO_TRAP, `MICROPC_NEGX_CLR_NEG_NOT_NBCD } :
2257
    // MOVE FROM SR
2258
    ( ir[15:6] == 10'b0100_0000_11 && ir[5:3] != 3'b001 && (ir[5:3] != 3'b111 || ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001)
2259
    ) ? { NO_TRAP, `MICROPC_MOVE_FROM_SR } :
2260
    // CHK
2261
    ( ir[15:12] == 4'b0100 && ir[8:6] == 3'b110 && ir[5:3] != 3'b001 &&
2262
        (ir[5:3] != 3'b111 ||
2263
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2264
    ) ? { NO_TRAP, `MICROPC_CHK } :
2265
    // LEA
2266
    ( ir[15:12] == 4'b0100 && ir[8:6] == 3'b111  && (ir[5:3] == 3'b010 || ir[5:3] == 3'b101 || ir[5:3] == 3'b110 || ir[5:3] == 3'b111) &&
2267
        (ir[5:3] != 3'b111 ||
2268
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011))
2269
    ) ? { NO_TRAP, `MICROPC_LEA } :
2270
    // MOVE TO CCR, MOVE TO SR
2271
    ( (ir[15:6] == 10'b0100_0100_11 || ir[15:6] == 10'b0100_0110_11) && ir[5:3] != 3'b001 &&
2272
        (ir[5:3] != 3'b111 ||
2273
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2274
    ) ? { NO_TRAP, `MICROPC_MOVE_TO_CCR_MOVE_TO_SR } :
2275
    // SWAP,EXT
2276
    ( ir[15:12] == 4'b0100 && (ir[11:3] == 9'b1000_01_000 || (ir[11:7] == 5'b1000_1 && ir[5:3] == 3'b000) ) ) ? { NO_TRAP, `MICROPC_SWAP_EXT } :
2277
    // PEA
2278
    ( ir[15:6] == 10'b0100_1000_01 && ir[5:3] != 3'b000 && (ir[5:3] == 3'b010 || ir[5:3] == 3'b101 || ir[5:3] == 3'b110 || ir[5:3] == 3'b111) &&
2279
        (ir[5:3] != 3'b111 ||
2280
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011))
2281
    ) ? { NO_TRAP, `MICROPC_PEA } :
2282
    // MOVEM register to memory, predecrement
2283
    ( ir[15:7] == 9'b0100_1000_1 && ir[5:3] == 3'b100 ) ? { NO_TRAP, `MICROPC_MOVEM_register_to_memory_predecrement } :
2284
    // MOVEM register to memory, control
2285
    ( ir[15:7] == 9'b0100_1000_1 && (ir[5:3] == 3'b010 || ir[5:3] == 3'b101 || ir[5:3] == 3'b110 || ir[5:3] == 3'b111) &&
2286
        (ir[5:3] != 3'b111 || ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001)
2287
    ) ? { NO_TRAP, `MICROPC_MOVEM_register_to_memory_control } :
2288
    // TST
2289
    ( ir[15:8] == 8'b0100_1010 && ir[7:6] != 2'b11 && ir[5:3] != 3'b001 &&
2290
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2291
    ) ? { NO_TRAP, `MICROPC_TST } :
2292
    // TAS
2293
    ( ir[15:6] == 10'b0100_1010_11 && ir[5:3] != 3'b001 &&
2294
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2295
    ) ? { NO_TRAP, `MICROPC_TAS } :
2296
    // MOVEM memory to register
2297
    ( ir[15:7] == 9'b0100_1100_1 && (ir[5:3] == 3'b010 || ir[5:3] == 3'b011 || ir[5:3] == 3'b101 || ir[5:3] == 3'b110 || ir[5:3] == 3'b111) &&
2298
        (ir[5:3] != 3'b111 ||
2299
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011))
2300
    ) ? { NO_TRAP, `MICROPC_MOVEM_memory_to_register } :
2301
    // TRAP
2302
    ( ir[15:4] == 12'b0100_1110_0100 ) ? { NO_TRAP, `MICROPC_TRAP } :
2303
    // LINK
2304
    ( ir[15:3] == 13'b0100_1110_0101_0 ) ? { NO_TRAP, `MICROPC_LINK } :
2305
    // UNLK
2306
    ( ir[15:3] == 13'b0100_1110_0101_1 ) ? { NO_TRAP, `MICROPC_ULNK } :
2307
    // MOVE USP to USP
2308
    ( ir[15:3] == 13'b0100_1110_0110_0 ) ? { NO_TRAP, `MICROPC_MOVE_USP_to_USP } :
2309
    // MOVE USP to An
2310
    ( ir[15:3] == 13'b0100_1110_0110_1 ) ? { NO_TRAP, `MICROPC_MOVE_USP_to_An } :
2311
    // RESET
2312
    ( ir[15:0] == 16'b0100_1110_0111_0000 ) ? { NO_TRAP, `MICROPC_RESET } :
2313
    // NOP
2314
    ( ir[15:0] == 16'b0100_1110_0111_0001 ) ? { NO_TRAP, `MICROPC_NOP } :
2315
    // STOP
2316
    ( ir[15:0] == 16'b0100_1110_0111_0010 ) ? { NO_TRAP, `MICROPC_STOP } :
2317
    // RTE,RTR
2318
    ( ir[15:0] == 16'b0100_1110_0111_0011 || ir[15:0] == 16'b0100_1110_0111_0111 ) ? { NO_TRAP, `MICROPC_RTE_RTR } :
2319
    // RTS
2320
    ( ir[15:0] == 16'b0100_1110_0111_0101 ) ? { NO_TRAP, `MICROPC_RTS } :
2321
    // TRAPV
2322
    ( ir[15:0] == 16'b0100_1110_0111_0110 ) ? { NO_TRAP, `MICROPC_TRAPV } :
2323
    // JSR
2324
    ( ir[15:6] == 10'b0100_1110_10 && (ir[5:3] == 3'b010 || ir[5:3] == 3'b101 || ir[5:3] == 3'b110 || ir[5:3] == 3'b111) &&
2325
        (ir[5:3] != 3'b111 ||
2326
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011))
2327
    ) ? { NO_TRAP, `MICROPC_JSR } :
2328
    // JMP
2329
    ( ir[15:6] == 10'b0100_1110_11 && (ir[5:3] == 3'b010 || ir[5:3] == 3'b101 || ir[5:3] == 3'b110 || ir[5:3] == 3'b111) &&
2330
        (ir[5:3] != 3'b111 ||
2331
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011))
2332
    ) ? { NO_TRAP, `MICROPC_JMP } :
2333
    // ADDQ,SUBQ not An
2334
    ( ir[15:12] == 4'b0101 && ir[7:6] != 2'b11 && ir[5:3] != 3'b001 &&
2335
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2336
    ) ? { NO_TRAP, `MICROPC_ADDQ_SUBQ_not_An } :
2337
    // ADDQ,SUBQ An
2338
    ( ir[15:12] == 4'b0101 && ir[7:6] != 2'b11 && ir[7:6] != 2'b00 && ir[5:3] == 3'b001 ) ? { NO_TRAP, `MICROPC_ADDQ_SUBQ_An } :
2339
    // Scc
2340
    ( ir[15:12] == 4'b0101 && ir[7:6] == 2'b11 && ir[5:3] != 3'b001 &&
2341
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2342
    ) ? { NO_TRAP, `MICROPC_Scc } :
2343
    // DBcc
2344
    ( ir[15:12] == 4'b0101 && ir[7:6] == 2'b11 && ir[5:3] == 3'b001 ) ? { NO_TRAP, `MICROPC_DBcc } :
2345
    // BSR
2346
    ( ir[15:12] == 4'b0110 && ir[11:8] == 4'b0001 ) ? { NO_TRAP, `MICROPC_BSR } :
2347
    // Bcc,BRA
2348
    ( ir[15:12] == 4'b0110 && ir[11:8] != 4'b0001 ) ? { NO_TRAP, `MICROPC_Bcc_BRA } :
2349
    // MOVEQ
2350
    ( ir[15:12] == 4'b0111 && ir[8] == 1'b0 ) ? { NO_TRAP, `MICROPC_MOVEQ } :
2351
    // CMP
2352
    ( (ir[15:12] == 4'b1011) && (ir[8:6] == 3'b000 || ir[8:6] == 3'b001 || ir[8:6] == 3'b010) &&
2353
        (ir[8:6] != 3'b000 || ir[5:3] != 3'b001) &&
2354
        (ir[5:3] != 3'b111 ||
2355
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2356
    ) ? { NO_TRAP, `MICROPC_CMP } :
2357
    // CMPA
2358
    ( (ir[15:12] == 4'b1011) && (ir[8:6] == 3'b011 || ir[8:6] == 3'b111) &&
2359
        (ir[5:3] != 3'b111 ||
2360
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2361
    ) ? { NO_TRAP, `MICROPC_CMPA } :
2362
    // CMPM
2363
    ( ir[15:12] == 4'b1011 && (ir[8:6] == 3'b100 || ir[8:6] == 3'b101 || ir[8:6] == 3'b110) && ir[5:3] == 3'b001) ? { NO_TRAP, `MICROPC_CMPM } :
2364
    // EOR
2365
    ( ir[15:12] == 4'b1011 && (ir[8:6] == 3'b100 || ir[8:6] == 3'b101 || ir[8:6] == 3'b110) && ir[5:3] != 3'b001 &&
2366
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2367
    ) ? { NO_TRAP, `MICROPC_EOR } :
2368
    // ADD to mem,SUB to mem,AND to mem,OR to mem
2369
    (     (ir[15:12] == 4'b1101 || ir[15:12] == 4'b1001 || ir[15:12] == 4'b1100 || ir[15:12] == 4'b1000) &&
2370
        (ir[8:4] == 5'b10001 || ir[8:4] == 5'b10010 || ir[8:4] == 5'b10011 ||
2371
         ir[8:4] == 5'b10101 || ir[8:4] == 5'b10110 || ir[8:4] == 5'b10111 ||
2372
         ir[8:4] == 5'b11001 || ir[8:4] == 5'b11010 || ir[8:4] == 5'b11011) &&
2373
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2374
    ) ? { NO_TRAP, `MICROPC_ADD_to_mem_SUB_to_mem_AND_to_mem_OR_to_mem } :
2375
    // ADD to Dn,SUB to Dn,AND to Dn,OR to Dn
2376
    (     (ir[15:12] == 4'b1101 || ir[15:12] == 4'b1001 || ir[15:12] == 4'b1100 || ir[15:12] == 4'b1000) &&
2377
        (ir[8:6] == 3'b000 || ir[8:6] == 3'b001 || ir[8:6] == 3'b010) &&
2378
        (ir[12] != 1'b1 || ir[8:6] != 3'b000 || ir[5:3] != 3'b001) && (ir[12] == 1'b1 || ir[5:3] != 3'b001) &&
2379
        (ir[5:3] != 3'b111 ||
2380
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2381
    ) ? { NO_TRAP, `MICROPC_ADD_to_Dn_SUB_to_Dn_AND_to_Dn_OR_to_Dn } :
2382
    // ADDA,SUBA
2383
    ( (ir[15:12] == 4'b1101 || ir[15:12] == 4'b1001) && (ir[8:6] == 3'b011 || ir[8:6] == 3'b111) &&
2384
        (ir[5:3] != 3'b111 ||
2385
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2386
    ) ? { NO_TRAP, `MICROPC_ADDA_SUBA } :
2387
    // ABCD,SBCD,ADDX,SUBX
2388
    (     ((ir[15:12] == 4'b1100 || ir[15:12] == 4'b1000) && ir[8:4] == 5'b10000) ||
2389
        ((ir[15:12] == 4'b1101 || ir[15:12] == 4'b1001) && (ir[8:4] == 5'b10000 || ir[8:4] == 5'b10100 || ir[8:4] == 5'b11000) ) ) ?
2390
        { NO_TRAP, `MICROPC_ABCD_SBCD_ADDX_SUBX } :
2391
    // EXG
2392
    ( ir[15:12] == 4'b1100 && (ir[8:3] == 6'b101000 || ir[8:3] == 6'b101001 || ir[8:3] == 6'b110001) ) ? { NO_TRAP, `MICROPC_EXG } :
2393
    // MULS,MULU,DIVS,DIVU
2394
    ( (ir[15:12] == 4'b1100 || ir[15:12] == 4'b1000) && ir[7:6] == 2'b11 && ir[5:3] != 3'b001 &&
2395
        (ir[5:3] != 3'b111 ||
2396
            (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001 || ir[5:0] == 6'b111_010 || ir[5:0] == 6'b111_011 || ir[5:0] == 6'b111_100))
2397
    ) ? { NO_TRAP, `MICROPC_MULS_MULU_DIVS_DIVU } :
2398
    // ASL,LSL,ROL,ROXL,ASR,LSR,ROR,ROXR all memory
2399
    ( ir[15:12] == 4'b1110 && ir[11] == 1'b0 && ir[7:6] == 2'b11 && ir[5:3] != 3'b000 && ir[5:3] != 3'b001 &&
2400
        (ir[5:3] != 3'b111 || (ir[5:0] == 6'b111_000 || ir[5:0] == 6'b111_001))
2401
    ) ?  { NO_TRAP, `MICROPC_ASL_LSL_ROL_ROXL_ASR_LSR_ROR_ROXR_all_memory } :
2402
    // ASL,LSL,ROL,ROXL,ASR,LSR,ROR,ROXR all immediate/register
2403
    ( ir[15:12] == 4'b1110 && (ir[7:6] == 2'b00 || ir[7:6] == 2'b01 || ir[7:6] == 2'b10) ) ?
2404
        { NO_TRAP, `MICROPC_ASL_LSL_ROL_ROXL_ASR_LSR_ROR_ROXR_all_immediate_register } :
2405
 
2406
    // else
2407
 
2408
    { ILLEGAL_INSTRUCTION_TRAP, UNUSED_MICROPC }
2409
;
2410
 
2411
// load ea
2412
assign load_ea =
2413
    (
2414
        (ea_type == `EA_TYPE_ALL && (ea_mod == 3'b000 || ea_mod == 3'b001 || (ea_mod == 3'b111 && ea_reg == 3'b100))) ||
2415
        (ea_type == `EA_TYPE_DATAALTER && ea_mod == 3'b000) ||
2416
        (ea_type == `EA_TYPE_DN_AN && (ea_mod == 3'b000 || ea_mod == 3'b001)) ||
2417
        (ea_type == `EA_TYPE_DATA && (ea_mod == 3'b000 || (ea_mod == 3'b111 && ea_reg == 3'b100)))
2418
    ) ? 9'd0 // no ea needed
2419
    :
2420
    (ea_mod == 3'b010 && (
2421
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROL_POSTINC || ea_type == `EA_TYPE_CONTROLALTER_PREDEC ||
2422
        ea_type == `EA_TYPE_CONTROL || ea_type == `EA_TYPE_DATAALTER || ea_type == `EA_TYPE_MEMORYALTER ||
2423
        ea_type == `EA_TYPE_DATA
2424
    )) ? `MICROPC_LOAD_EA_An // (An)
2425
    :
2426
    (ea_mod == 3'b011 && (
2427
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROL_POSTINC || ea_type == `EA_TYPE_MEMORYALTER ||
2428
        ea_type == `EA_TYPE_DATAALTER || ea_type == `EA_TYPE_DATA
2429
    )) ? `MICROPC_LOAD_EA_An_plus // (An)+
2430
    :
2431
    (ea_mod == 3'b100 && (
2432
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROLALTER_PREDEC || ea_type == `EA_TYPE_DATAALTER ||
2433
        ea_type == `EA_TYPE_MEMORYALTER ||    ea_type == `EA_TYPE_DATA
2434
    )) ? `MICROPC_LOAD_EA_minus_An // -(An)
2435
    :
2436
    (ea_mod == 3'b101 && (
2437
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROL_POSTINC || ea_type == `EA_TYPE_CONTROLALTER_PREDEC ||
2438
        ea_type == `EA_TYPE_CONTROL ||    ea_type == `EA_TYPE_DATAALTER || ea_type == `EA_TYPE_MEMORYALTER || ea_type == `EA_TYPE_DATA
2439
    )) ? `MICROPC_LOAD_EA_d16_An // (d16, An)
2440
    :
2441
    (ea_mod == 3'b110 && (
2442
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROL_POSTINC || ea_type == `EA_TYPE_CONTROLALTER_PREDEC ||
2443
        ea_type == `EA_TYPE_CONTROL || ea_type == `EA_TYPE_DATAALTER || ea_type == `EA_TYPE_MEMORYALTER || ea_type == `EA_TYPE_DATA
2444
    )) ? `MICROPC_LOAD_EA_d8_An_Xn // (d8, An, Xn)
2445
    :
2446
    (ea_mod == 3'b111 && ea_reg == 3'b000 && (
2447
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROL_POSTINC || ea_type == `EA_TYPE_CONTROLALTER_PREDEC ||
2448
        ea_type == `EA_TYPE_CONTROL ||    ea_type == `EA_TYPE_DATAALTER || ea_type == `EA_TYPE_MEMORYALTER || ea_type == `EA_TYPE_DATA
2449
    )) ? `MICROPC_LOAD_EA_xxx_W // (xxx).W
2450
    :
2451
    (ea_mod == 3'b111 && ea_reg == 3'b001 && (
2452
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROL_POSTINC || ea_type == `EA_TYPE_CONTROLALTER_PREDEC ||
2453
        ea_type == `EA_TYPE_CONTROL || ea_type == `EA_TYPE_DATAALTER || ea_type == `EA_TYPE_MEMORYALTER || ea_type == `EA_TYPE_DATA
2454
    )) ? `MICROPC_LOAD_EA_xxx_L // (xxx).L
2455
    :
2456
    (ea_mod == 3'b111 && ea_reg == 3'b010 && (
2457
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROL_POSTINC || ea_type == `EA_TYPE_CONTROL || ea_type == `EA_TYPE_DATA
2458
    )) ? `MICROPC_LOAD_EA_d16_PC // (d16, PC)
2459
    :
2460
    (ea_mod == 3'b111 && ea_reg == 3'b011 && (
2461
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROL_POSTINC || ea_type == `EA_TYPE_CONTROL || ea_type == `EA_TYPE_DATA
2462
    )) ? `MICROPC_LOAD_EA_d8_PC_Xn // (d8, PC, Xn)
2463
    :
2464
    `MICROPC_LOAD_EA_illegal_command // illegal command
2465
;
2466
 
2467
// perform ea read
2468
assign perform_ea_read =
2469
    ( ea_mod == 3'b000 && (ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_DATAALTER || ea_type == `EA_TYPE_DN_AN ||
2470
      ea_type == `EA_TYPE_DATA) ) ?
2471
        `MICROPC_PERFORM_EA_READ_Dn :
2472
    ( ea_mod == 3'b001 && (ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_DN_AN) ) ? `MICROPC_PERFORM_EA_READ_An :
2473
    ( ea_mod == 3'b111 && ea_reg == 3'b100 && (ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_DATA) ) ?
2474
        `MICROPC_PERFORM_EA_READ_imm :
2475
    `MICROPC_PERFORM_EA_READ_memory
2476
;
2477
 
2478
// perform ea write
2479
assign perform_ea_write =
2480
    ( ea_mod == 3'b000 && (ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_DATAALTER || ea_type == `EA_TYPE_DN_AN ||
2481
      ea_type == `EA_TYPE_DATA) ) ?
2482
        `MICROPC_PERFORM_EA_WRITE_Dn :
2483
    ( ea_mod == 3'b001 && (ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_DN_AN) ) ? `MICROPC_PERFORM_EA_WRITE_An :
2484
    `MICROPC_PERFORM_EA_WRITE_memory
2485
;
2486
 
2487
// save ea
2488
assign save_ea =
2489
    (ea_mod == 3'b011 && (
2490
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROL_POSTINC || ea_type == `EA_TYPE_MEMORYALTER ||
2491
        ea_type == `EA_TYPE_DATAALTER || ea_type == `EA_TYPE_DATA
2492
    )) ? `MICROPC_SAVE_EA_An_plus // (An)+
2493
    :
2494
    (ea_mod == 3'b100 && (
2495
        ea_type == `EA_TYPE_ALL || ea_type == `EA_TYPE_CONTROLALTER_PREDEC || ea_type == `EA_TYPE_DATAALTER ||
2496
        ea_type == `EA_TYPE_MEMORYALTER || ea_type == `EA_TYPE_DATA
2497
    )) ? `MICROPC_SAVE_EA_minus_An // -(An)
2498
    :
2499
    9'd0 // no ea needed
2500
;
2501
 
2502
endmodule
2503
 
2504
/***********************************************************************************************************************
2505
 * Condition
2506
 **********************************************************************************************************************/
2507
 
2508
/*! \brief Condition tests.
2509
 *
2510
 * The condition module implements the condition tests of the MC68000. Its inputs are the condition codes
2511
 * and the currently selected test. The output is binary: the test is true or false. The output of the condition module
2512
 * is an input to the microcode_branch module, that decides which microcode word to execute next.
2513
 */
2514
module condition(
2515
    input [3:0] cond,
2516
    input [7:0] ccr,
2517
    output condition
2518
);
2519
 
2520
wire C,V,Z,N;
2521
assign C = ccr[0];
2522
assign V = ccr[1];
2523
assign Z = ccr[2];
2524
assign N = ccr[3];
2525
 
2526
assign condition =  (cond == 4'b0000) ? 1'b1 :                              // true
2527
                    (cond == 4'b0001) ? 1'b0 :                              // false
2528
                    (cond == 4'b0010) ? ~C & ~Z    :                        // high
2529
                    (cond == 4'b0011) ? C | Z :                             // low or same
2530
                    (cond == 4'b0100) ? ~C :                                // carry clear
2531
                    (cond == 4'b0101) ? C :                                 // carry set
2532
                    (cond == 4'b0110) ? ~Z :                                // not equal
2533
                    (cond == 4'b0111) ? Z :                                 // equal
2534
                    (cond == 4'b1000) ? ~V :                                // overflow clear
2535
                    (cond == 4'b1001) ? V :                                 // overflow set
2536
                    (cond == 4'b1010) ? ~N :                                // plus
2537
                    (cond == 4'b1011) ? N :                                 // minus
2538
                    (cond == 4'b1100) ? (N & V) | (~N & ~V) :               // greater or equal
2539
                    (cond == 4'b1101) ? (N & ~V) | (~N & V)    :            // less than
2540
                    (cond == 4'b1110) ? (N & V & ~Z) | (~N & ~V & ~Z) :     // greater than
2541
                    (cond == 4'b1111) ? (Z) | (N & ~V) | (~N & V) :         // less or equal
2542
                    1'b0;
2543
endmodule
2544
 
2545
/***********************************************************************************************************************
2546
 * ALU
2547
 **********************************************************************************************************************/
2548
 
2549
/*! \brief Arithmetic and Logic Unit.
2550
 *
2551
 * The alu module is responsible for performing all of the arithmetic and logic operations of the ao68000 processor.
2552
 * It operates on two 32-bit registers: operand1 and operand2 from the registers module. The output is saved into
2553
 * a result 32-bit register. This register is located in the alu module.
2554
 *
2555
 * The alu module also contains the status register (SR) with the condition code register. The microcode decides what
2556
 * operation the alu performs.
2557
 */
2558
module alu(
2559
    input clock,
2560
    input reset_n,
2561
 
2562
    // only zero bit
2563
    input [31:0] address,
2564
    // only ir[11:9] and ir[6]
2565
    input [15:0] ir,
2566
    // byte 2'b00, word 2'b01, long 2'b10
2567 13 alfik
    input [2:0] size,
2568 12 alfik
 
2569
    input [31:0] operand1,
2570
    input [31:0] operand2,
2571
 
2572
    input [2:0] interrupt_mask,
2573
    input [4:0] alu_control,
2574
 
2575
    output reg [15:0] sr,
2576
    output reg [31:0] result,
2577 13 alfik
 
2578
    output reg alu_signal,
2579
    output alu_mult_div_ready
2580 12 alfik
);
2581
 
2582 13 alfik
//****************************************************** Altera-specific multiplication and division modules START
2583
/* Multiplication and division modules.
2584
 *
2585
 * Currently this module contains:
2586
 * - <em>lpm_mult</em> instantiation from Altera Megafunction/LPM library,
2587
 * - a sequential state machine for division written by Frederic Requin
2588
 */
2589
 
2590
wire        mult_div_sign = ir[8];
2591
 
2592
// 18-2 - division calculation, 1 - waiting for result read, 0 - idle
2593
reg  [4:0]  div_count;
2594
reg [16:0]  quotient;
2595
reg [31:0]  dividend, divider;
2596
 
2597
// Compute the difference with borrow
2598
wire [32:0] div_diff = (dividend - divider);
2599
 
2600
// Overflow flag: when (quotient >= 65536) or (signed division and (quotient >= 32768 or quotient < -32768))
2601
wire        div_overflow =
2602
    (quotient[16] == 1'b1 ||
2603
        (mult_div_sign == 1'b1 && (
2604
            ((operand1[31] ^ operand2[15]) == 1'b0 && quotient[15] == 1'b1) ||
2605
            ((operand1[31] ^ operand2[15]) == 1'b1 && quotient[15:0] > 16'd32768) )));
2606
 
2607
wire [15:0] div_quotient =
2608
    // positive quotient
2609
    (((operand1[31] ^ operand2[15]) & mult_div_sign) == 1'b0)? quotient[15:0] :
2610
    // negative quotient
2611
    -quotient[15:0];
2612
 
2613
wire [15:0] div_remainder =
2614
    // positive remainder
2615
    ((operand1[31] & mult_div_sign) == 1'b0)? dividend[15:0] :
2616
    // negative remainder
2617
    -dividend[15:0];
2618
 
2619
always @(posedge clock or negedge reset_n) begin
2620
    if(reset_n == 1'b0) begin
2621
        div_count <= 5'd0;
2622
    end
2623
    // Cycle #0 : load the registers
2624
    else if(alu_control == `ALU_MULS_MULU_DIVS_DIVU && ir[15:12] == 4'b1000 && div_count == 5'd0) begin
2625
        // 17 cycles to finish + wait state
2626
        div_count   <= 5'd18;
2627
        // Clear the quotient
2628
        quotient    <= 17'd0;
2629
 
2630
        // Unsigned divide or positive numerator
2631
        if ((!mult_div_sign) || (!operand1[31]))    dividend <= operand1;
2632
        // Negative numerator
2633
        else                                        dividend <= -operand1;
2634
 
2635
        // Unsigned divide or positive denominator
2636
        if ((!mult_div_sign) || (!operand2[15]))    divider <= {operand2[15:0],16'd0};
2637
        // Negative denominator
2638
        else                                        divider <= {-operand2[15:0],16'd0};
2639
    end
2640
    // Cycles #1-17 : division calculation
2641
    else if(div_count > 5'd1) begin
2642
        // Check difference's sign
2643
        if (!div_diff[32]) begin
2644
          // Difference is positive : shift a one
2645
          dividend <= div_diff[31:0];
2646
          quotient <= {quotient[15:0], 1'b1};
2647
        end
2648
        else begin
2649
          // Difference is negative : shift a zero
2650
          quotient <= {quotient[15:0], 1'b0};
2651
        end
2652
        // Shift right divider
2653
        divider <= {1'b0, divider[31:1]};
2654
        // Count one bit
2655
        div_count <= div_count - 5'd1;
2656
    end
2657
    // result read
2658
    else if(alu_control == `ALU_MULS_MULU_DIVS_DIVU && ir[15:12] == 4'b1000 && div_count == 5'd1) begin
2659
        // goto idle
2660
        div_count <= div_count - 5'd1;
2661
    end
2662
end
2663
 
2664
// MULS/MULU: 16-bit operand1[15:0] signed/unsigned * operand2[15:0] signed/unsigned = 32-bit result signed/unsigned
2665
// Optimization by Frederic Requin
2666
wire [33:0] mult_result;
2667
 
2668
lpm_mult muls(
2669
    .clock  (clock),
2670
    .dataa  ({operand1[15] & mult_div_sign, operand1[15:0]}),
2671
    .datab  ({operand2[15] & mult_div_sign, operand2[15:0]}),
2672
    .result (mult_result)
2673
);
2674
defparam
2675
    muls.lpm_widtha = 17,
2676
    muls.lpm_widthb = 17,
2677
    muls.lpm_widthp = 34,
2678
    muls.lpm_representation = "SIGNED",
2679
    muls.lpm_pipeline = 1;
2680
 
2681
// multiplication ready in one cycle, division ready when div_count in waiting or idle state
2682
assign alu_mult_div_ready = (div_count == 5'd1 || div_count == 5'd0);
2683
 
2684 12 alfik
//****************************************************** Altera-specific multiplication and division modules END
2685
 
2686
// ALU internal defines
2687 13 alfik
`define Sm ((size[0] == 1'b1) ? operand2[7] :           (size[1] == 1'b1) ? operand2[15] :            operand2[31])
2688 12 alfik
 
2689 13 alfik
`define Dm ((size[0] == 1'b1) ? operand1[7] :           (size[1] == 1'b1) ? operand1[15] :            operand1[31])
2690 12 alfik
 
2691 13 alfik
`define Rm ((size[0] == 1'b1) ? result[7] :             (size[1] == 1'b1) ? result[15] :              result[31])
2692 12 alfik
 
2693 13 alfik
`define Z  ((size[0] == 1'b1) ? (result[7:0] == 8'b0) : (size[1] == 1'b1) ? (result[15:0] == 16'b0) : (result[31:0] == 32'b0))
2694 12 alfik
 
2695
// ALU operations
2696
 
2697
reg [2:0] interrupt_mask_copy;
2698
reg was_interrupt;
2699
 
2700
always @(posedge clock or negedge reset_n) begin
2701
    if(reset_n == 1'b0) begin
2702
        sr <= { 1'b0, 1'b0, 1'b1, 2'b0, 3'b111, 8'b0 };
2703
        result <= 32'd0;
2704 13 alfik
        alu_signal <= 1'b0;
2705 12 alfik
        interrupt_mask_copy <= 3'b0;
2706
        was_interrupt <= 1'b0;
2707
    end
2708
    else begin
2709
        case(alu_control)
2710
            `ALU_SR_SET_INTERRUPT: begin
2711
                interrupt_mask_copy <= interrupt_mask[2:0];
2712
                was_interrupt <= 1'b1;
2713
            end
2714
 
2715
            `ALU_SR_SET_TRAP: begin
2716
                if(was_interrupt == 1'b1) begin
2717
                    sr <= { 1'b0, sr[14], 1'b1, sr[12:11], interrupt_mask_copy[2:0], sr[7:0] };
2718
                end
2719
                else begin
2720
                    sr <= { 1'b0, sr[14], 1'b1, sr[12:0] };
2721
                end
2722
                was_interrupt <= 1'b0;
2723
            end
2724
 
2725
            `ALU_MOVEP_M2R_1: begin
2726
                if(ir[6] == 1'b1)   result[31:24] <= operand1[7:0];
2727
                else                result[15:8] <= operand1[7:0];
2728
                //CCR: no change
2729
            end
2730
            `ALU_MOVEP_M2R_2: begin
2731
                if(ir[6] == 1'b1)   result[23:16] <= operand1[7:0];
2732
                else                result[7:0] <= operand1[7:0];
2733
                //CCR: no change
2734
            end
2735
            `ALU_MOVEP_M2R_3: begin
2736
                if(ir[6] == 1'b1)   result[15:8] <= operand1[7:0];
2737
                //CCR: no change
2738
            end
2739
            `ALU_MOVEP_M2R_4: begin
2740
                if(ir[6] == 1'b1)   result[7:0] <= operand1[7:0];
2741
                //CCR: no change
2742
            end
2743
 
2744
 
2745
            `ALU_MOVEP_R2M_1: begin
2746
                if(ir[6] == 1'b1)   result[7:0] <= operand1[31:24];
2747
                else                result[7:0] <= operand1[15:8];
2748
                // CCR: no change
2749
            end
2750
            `ALU_MOVEP_R2M_2: begin
2751
                if(ir[6] == 1'b1)   result[7:0] <= operand1[23:16];
2752
                else                result[7:0] <= operand1[7:0];
2753
                // CCR: no change
2754
            end
2755
            `ALU_MOVEP_R2M_3: begin
2756
                result[7:0] <= operand1[15:8];
2757
                // CCR: no change
2758
            end
2759
            `ALU_MOVEP_R2M_4: begin
2760
                result[7:0] <= operand1[7:0];
2761
                // CCR: no change
2762
            end
2763
 
2764
 
2765
 
2766
            `ALU_SIGN_EXTEND: begin
2767
                // move operand1 with sign-extension to result
2768 13 alfik
                if(size[1] == 1'b1) begin
2769 12 alfik
                    result <= { {16{operand1[15]}}, operand1[15:0] };
2770
                end
2771
                else begin
2772
                    result <= operand1;
2773
                end
2774
                // CCR: no change
2775
            end
2776
 
2777
            `ALU_ARITHMETIC_LOGIC: begin
2778
 
2779
                // OR,OR to mem,OR to Dn
2780
                if(         (ir[15:12] == 4'b0000 && ir[11:9] == 3'b000) ||
2781
                            (ir[15:12] == 4'b1000)
2782
                )             result[31:0] = operand1[31:0] | operand2[31:0];
2783
                // AND,AND to mem,AND to Dn
2784
                else if(     (ir[15:12] == 4'b0000 && ir[11:9] == 3'b001) ||
2785
                            (ir[15:12] == 4'b1100)
2786
                )             result[31:0] = operand1[31:0] & operand2[31:0];
2787
                // EORI,EOR
2788
                else if(     (ir[15:12] == 4'b0000 && ir[11:9] == 3'b101) ||
2789
                            (ir[15:12] == 4'b1011 && (ir[8:6] == 3'b100 || ir[8:6] == 3'b101 || ir[8:6] == 3'b110) && ir[5:3] != 3'b001)
2790
                )            result[31:0] = operand1[31:0] ^ operand2[31:0];
2791
                // ADD,ADD to mem,ADD to Dn,ADDQ
2792
                else if(     (ir[15:12] == 4'b0000 && ir[11:9] == 3'b011) ||
2793
                            (ir[15:12] == 4'b1101) ||
2794
                            (ir[15:12] == 4'b0101 && ir[8] == 1'b0)
2795
                )             result[31:0] = operand1[31:0] + operand2[31:0];
2796
                // SUBI,CMPI,CMPM,SUB to mem,SUB to Dn,CMP,SUBQ
2797
                else if(     (ir[15:12] == 4'b0000 && ir[11:9] == 3'b010) ||
2798
                            (ir[15:12] == 4'b0000 && ir[11:9] == 3'b110) ||
2799
                            (ir[15:12] == 4'b1011 && (ir[8:6] == 3'b100 || ir[8:6] == 3'b101 || ir[8:6] == 3'b110) && ir[5:3] == 3'b001)     ||
2800
                            (ir[15:12] == 4'b1001) ||
2801
                            (ir[15:12] == 4'b1011 && (ir[8:6] == 3'b000 || ir[8:6] == 3'b001 || ir[8:6] == 3'b010)) ||
2802
                            (ir[15:12] == 4'b0101 && ir[8] == 1'b1)
2803
                )            result[31:0] = operand1[31:0] - operand2[31:0];
2804
 
2805
                // Z
2806
                sr[2] <= `Z;
2807
                // N
2808
                sr[3] <= `Rm;
2809
 
2810
                // CMPI,CMPM,CMP
2811
                if( (ir[15:12] == 4'b0000 && ir[11:9] == 3'b110) ||
2812
                    (ir[15:12] == 4'b1011 && (ir[8:6] == 3'b100 || ir[8:6] == 3'b101 || ir[8:6] == 3'b110) && ir[5:3] == 3'b001) ||
2813
                    (ir[15:12] == 4'b1011 && (ir[8:6] == 3'b000 || ir[8:6] == 3'b001 || ir[8:6] == 3'b010))
2814
                ) begin
2815
                    // C,V
2816
                    sr[0] <= (`Sm & ~`Dm) | (`Rm & ~`Dm) | (`Sm & `Rm);
2817
                    sr[1] <= (~`Sm & `Dm & ~`Rm) | (`Sm & ~`Dm & `Rm);
2818
                    // X not affected
2819
                end
2820
                // ADDI,ADD to mem,ADD to Dn,ADDQ
2821
                else if(     (ir[15:12] == 4'b0000 && ir[11:9] == 3'b011) ||
2822
                            (ir[15:12] == 4'b1101) ||
2823
                            (ir[15:12] == 4'b0101 && ir[8] == 1'b0)
2824
                ) begin
2825
                    // C,X,V
2826
                    sr[0] <= (`Sm & `Dm) | (~`Rm & `Dm) | (`Sm & ~`Rm);
2827
                    sr[4] <= (`Sm & `Dm) | (~`Rm & `Dm) | (`Sm & ~`Rm); //=ccr[0];
2828
                    sr[1] <= (`Sm & `Dm & ~`Rm) | (~`Sm & ~`Dm & `Rm);
2829
                end
2830
                // SUBI,SUB to mem,SUB to Dn,SUBQ
2831
                else if(     (ir[15:12] == 4'b0000 && ir[11:9] == 3'b010) ||
2832
                            (ir[15:12] == 4'b1001) ||
2833
                            (ir[15:12] == 4'b0101 && ir[8] == 1'b1)
2834
                ) begin
2835
                    // C,X,V
2836
                    sr[0] <= (`Sm & ~`Dm) | (`Rm & ~`Dm) | (`Sm & `Rm);
2837
                    sr[4] <= (`Sm & ~`Dm) | (`Rm & ~`Dm) | (`Sm & `Rm); //=ccr[0];
2838
                    sr[1] <= (~`Sm & `Dm & ~`Rm) | (`Sm & ~`Dm & `Rm);
2839
                end
2840
                // ANDI,EORI,ORI,EOR,OR to mem,AND to mem,OR to Dn,AND to Dn
2841
                else begin
2842
                    // C,V
2843
                    sr[0] <= 1'b0;
2844
                    sr[1] <= 1'b0;
2845
                    // X not affected
2846
                end
2847
            end
2848
 
2849
            `ALU_ABCD_SBCD_ADDX_SUBX: begin // 259 LE
2850
                // ABCD
2851
                if( ir[14:12] == 3'b100 ) begin
2852
                    result[13:8] = {1'b0, operand1[3:0]} + {1'b0, operand2[3:0]} + {4'b0, sr[4]};
2853
                    result[19:14] = {1'b0, operand1[7:4]} + {1'b0, operand2[7:4]};
2854
 
2855
                    result[31:23] = operand1[7:0] + operand2[7:0] + {7'b0, sr[4]};
2856
 
2857
                    result[13:8] = (result[13:8] > 6'd9) ? (result[13:8] + 6'd6) : result[13:8];
2858
                    result[19:14] = (result[13:8] > 6'h1F) ? (result[19:14] + 6'd2) :
2859
                                    (result[13:8] > 6'h0F) ? (result[19:14] + 6'd1) :
2860
                                    result[19:14];
2861
                    result[19:14] = (result[19:14] > 6'd9) ? (result[19:14] + 6'd6) : result[19:14];
2862
 
2863
                    result[7:4] = result[17:14];
2864
                    result[3:0] = result[11:8];
2865
 
2866
                    // C
2867
                    sr[0] <= (result[19:14] > 6'd9) ? 1'b1 : 1'b0;
2868
                    // X = C
2869
                    sr[4] <= (result[19:14] > 6'd9) ? 1'b1 : 1'b0;
2870
 
2871
                    // V
2872
                    sr[1] <= (result[30] == 1'b0 && result[7] == 1'b1) ? 1'b1 : 1'b0;
2873
                end
2874
                // SBCD
2875
                else if( ir[14:12] == 3'b000 ) begin
2876
 
2877
                    result[13:8] = 6'd32 + {2'b0, operand1[3:0]} - {2'b0, operand2[3:0]} - {5'b0, sr[4]};
2878
                    result[19:14] = 6'd32 + {2'b0, operand1[7:4]} - {2'b0, operand2[7:4]};
2879
 
2880
                    result[31:23] = operand1[7:0] - operand2[7:0] - {7'b0, sr[4]};
2881
 
2882
                    result[13:8] = (result[13:8] < 6'd32) ? (result[13:8] - 6'd6) : result[13:8];
2883
                    result[19:14] = (result[13:8] < 6'd16) ? (result[19:14] - 6'd2) :
2884
                                    (result[13:8] < 6'd32) ? (result[19:14] - 6'd1) :
2885
                                    result[19:14];
2886
                    result[19:14] = (result[19:14] < 6'd32 && result[31] == 1'b1) ? (result[19:14] - 6'd6) : result[19:14];
2887
 
2888
                    result[7:4] = result[17:14];
2889
                    result[3:0] = result[11:8];
2890
 
2891
                    // C
2892
                    sr[0] <= (result[19:14] < 6'd32) ? 1'b1 : 1'b0;
2893
                    // X = C
2894
                    sr[4] <= (result[19:14] < 6'd32) ? 1'b1 : 1'b0;
2895
 
2896
                    // V
2897
                    sr[1] <= (result[30] == 1'b1 && result[7] == 1'b0) ? 1'b1 : 1'b0;
2898
                end
2899
                // ADDX
2900
                else if( ir[14:12] == 3'b101 ) result[31:0] = operand1[31:0] + operand2[31:0] + sr[4];
2901
                // SUBX
2902
                else if( ir[14:12] == 3'b001 ) result[31:0] = operand1[31:0] - operand2[31:0] - sr[4];
2903
 
2904
                // Z
2905
                sr[2] <= sr[2] & `Z;
2906
                // N
2907
                sr[3] <= `Rm;
2908
 
2909
                // ADDX
2910
                if(ir[14:12] == 3'b101 ) begin
2911
                    // C,X,V
2912
                    sr[0] <= (`Sm & `Dm) | (~`Rm & `Dm) | (`Sm & ~`Rm);
2913
                    sr[4] <= (`Sm & `Dm) | (~`Rm & `Dm) | (`Sm & ~`Rm); //=ccr[0];
2914
                    sr[1] <= (`Sm & `Dm & ~`Rm) | (~`Sm & ~`Dm & `Rm);
2915
                end
2916
                // SUBX
2917
                else if(ir[14:12] == 3'b001 ) begin
2918
                    // C,X,V
2919
                    sr[0] <= (`Sm & ~`Dm) | (`Rm & ~`Dm) | (`Sm & `Rm);
2920
                    sr[4] <= (`Sm & ~`Dm) | (`Rm & ~`Dm) | (`Sm & `Rm); //=ccr[0];
2921
                    sr[1] <= (~`Sm & `Dm & ~`Rm) | (`Sm & ~`Dm & `Rm);
2922
                end
2923
            end
2924
 
2925
            `ALU_ASL_LSL_ROL_ROXL_ASR_LSR_ROR_ROXR_prepare: begin
2926
 
2927 13 alfik
                if(size[0] == 1'b1)         result[7:0] = operand1[7:0];
2928
                else if(size[1] == 1'b1)    result[15:0] = operand1[15:0];
2929
                else if(size[2] == 1'b1)    result[31:0] = operand1[31:0];
2930 12 alfik
 
2931
                // X for ASL
2932
                //if(operand2[5:0] > 6'b0 && ir[8] == 1'b1 && ((ir[7:6] == 2'b11 && ir[10:9] == 2'b00) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b00)) ) begin
2933
                    // X set to Dm
2934
                //    sr[4] <= `Dm;
2935
                //end
2936
                // else X not affected
2937
 
2938
                // V cleared
2939
                sr[1] <= 1'b0;
2940
                // C for ROXL,ROXR: set to X
2941
                if( (ir[7:6] == 2'b11 && ir[10:9] == 2'b10) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b10) ) begin
2942
                    sr[0] <= sr[4];
2943
                end
2944
                else begin
2945
                    // C cleared
2946
                    sr[0] <= 1'b0;
2947
                end
2948
 
2949
                // N set
2950
                sr[3] <= `Rm;
2951
                // Z set
2952
                sr[2] <= `Z;
2953
            end
2954
 
2955
            `ALU_ASL_LSL_ROL_ROXL_ASR_LSR_ROR_ROXR: begin
2956
 
2957
                // ASL
2958
                if( ((ir[7:6] == 2'b11 && ir[10:9] == 2'b00) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b00)) && ir[8] == 1'b1) begin
2959
                    result[31:0] = {operand1[30:0], 1'b0};
2960
 
2961
                    sr[1] <= (sr[1] == 1'b0)? (`Rm != `Dm) : 1'b1; // V
2962
                    sr[0] <= `Dm;           // C
2963
                    sr[4] <= `Dm;           // X
2964
                end
2965
                // LSL
2966
                else if( ((ir[7:6] == 2'b11 && ir[10:9] == 2'b01) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b01)) && ir[8] == 1'b1) begin
2967
                    result[31:0] = {operand1[30:0], 1'b0};
2968
 
2969
                    sr[1] <= 1'b0;          // V
2970
                    sr[0] <= `Dm;           // C
2971
                    sr[4] <= `Dm;           // X
2972
                end
2973
                // ROL
2974
                else if( ((ir[7:6] == 2'b11 && ir[10:9] == 2'b11) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b11)) && ir[8] == 1'b1) begin
2975
                    result[31:0] = {operand1[30:0], `Dm};
2976
 
2977
                    sr[1] <= 1'b0;          // V
2978
                    sr[0] <= `Dm;           // C
2979
                                            // X not affected
2980
                end
2981
                // ROXL
2982
                else if( ((ir[7:6] == 2'b11 && ir[10:9] == 2'b10) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b10)) && ir[8] == 1'b1) begin
2983
                    result[31:0] = {operand1[30:0], sr[4]};
2984
 
2985
                    sr[1] <= 1'b0;          // V
2986
                    sr[0] <= `Dm;           // C
2987
                    sr[4] <= `Dm;           // X
2988
                end
2989
                // ASR
2990
                else if( ((ir[7:6] == 2'b11 && ir[10:9] == 2'b00) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b00)) && ir[8] == 1'b0) begin
2991 13 alfik
                    if(size[0] == 1'b1)         result[7:0] = { operand1[7], operand1[7:1] };
2992
                    else if(size[1] == 1'b1)    result[15:0] = { operand1[15], operand1[15:1] };
2993
                    else if(size[2] == 1'b1)    result[31:0] = { operand1[31], operand1[31:1] };
2994 12 alfik
 
2995
                    sr[1] <= 1'b0;          // V
2996
                    sr[0] <= operand1[0];   // C
2997
                    sr[4] <= operand1[0];   // X
2998
                end
2999
                // LSR
3000
                else if( ((ir[7:6] == 2'b11 && ir[10:9] == 2'b01) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b01)) && ir[8] == 1'b0) begin
3001 13 alfik
                    if(size[0] == 1'b1)         result[7:0] = { 1'b0, operand1[7:1] };
3002
                    else if(size[1] == 1'b1)    result[15:0] = { 1'b0, operand1[15:1] };
3003
                    else if(size[2] == 1'b1)    result[31:0] = { 1'b0, operand1[31:1] };
3004 12 alfik
 
3005
                    sr[1] <= 1'b0;          // V
3006
                    sr[0] <= operand1[0];   // C
3007
                    sr[4] <= operand1[0];   // X
3008
                end
3009
                // ROR
3010
                else if( ((ir[7:6] == 2'b11 && ir[10:9] == 2'b11) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b11)) && ir[8] == 1'b0) begin
3011 13 alfik
                    if(size[0] == 1'b1)         result[7:0] = { operand1[0], operand1[7:1] };
3012
                    else if(size[1] == 1'b1)    result[15:0] = { operand1[0], operand1[15:1] };
3013
                    else if(size[2] == 1'b1)    result[31:0] = { operand1[0], operand1[31:1] };
3014 12 alfik
 
3015
                    sr[1] <= 1'b0;          // V
3016
                    sr[0] <= operand1[0];   // C
3017
                    // X not affected
3018
                end
3019
                // ROXR
3020
                else if( ((ir[7:6] == 2'b11 && ir[10:9] == 2'b10) || (ir[7:6] != 2'b11 && ir[4:3] == 2'b10)) && ir[8] == 1'b0) begin
3021 13 alfik
                    if(size[0] == 1'b1)         result[7:0] = {sr[4], operand1[7:1]};
3022
                    else if(size[1] == 1'b1)    result[15:0] = {sr[4], operand1[15:1]};
3023
                    else if(size[2] == 1'b1)    result[31:0] = {sr[4], operand1[31:1]};
3024 12 alfik
 
3025
                    sr[1] <= 1'b0;          // V
3026
                    sr[0] <= operand1[0];   // C
3027
                    sr[4] <= operand1[0];   // X
3028
                end
3029
 
3030
                // N set
3031
                sr[3] <= `Rm;
3032
                // Z set
3033
                sr[2] <= `Z;
3034
            end
3035
 
3036
            `ALU_MOVE: begin
3037
                result = operand1;
3038
 
3039
                // X not affected
3040
                // C cleared
3041
                sr[0] <= 1'b0;
3042
                // V cleared
3043
                sr[1] <= 1'b0;
3044
 
3045
                // N set
3046
                sr[3] <= `Rm;
3047
                // Z set
3048
                sr[2] <= `Z;
3049
            end
3050
 
3051
            `ALU_ADDA_SUBA_CMPA_ADDQ_SUBQ: begin
3052
                // ADDA: 1101
3053
                // CMPA: 1011
3054
                // SUBA: 1001
3055
                // ADDQ,SUBQ: 0101 xxx0,1
3056
                // operation requires that operand2 was sign extended
3057
 
3058
                // ADDA,ADDQ
3059
                if( ir[15:12] == 4'b1101 || (ir[15:12] == 4'b0101 && ir[8] == 1'b0) )
3060
                    result[31:0] = operand1[31:0] + operand2[31:0];
3061
                // SUBA,CMPA,SUBQ
3062
                else if( ir[15:12] == 4'b1001 || ir[15:12] == 4'b1011 || (ir[15:12] == 4'b0101 && ir[8] == 1'b1) )
3063
                    result[31:0] = operand1[31:0] - operand2[31:0];
3064
 
3065
                // for CMPA
3066
                if( ir[15:12] == 4'b1011 ) begin
3067
                    // Z
3068
                    sr[2] <= `Z;
3069
                    // N
3070
                    sr[3] <= `Rm;
3071
 
3072
                    // C,V
3073
                    sr[0] <= (`Sm & ~`Dm) | (`Rm & ~`Dm) | (`Sm & `Rm);
3074
                    sr[1] <= (~`Sm & `Dm & ~`Rm) | (`Sm & ~`Dm & `Rm);
3075
                    // X not affected
3076
                end
3077
                // for ADDA,SUBA,ADDQ,SUBQ: ccr not affected
3078
            end
3079
 
3080
            `ALU_CHK: begin
3081
                result[15:0] = operand1[15:0] - operand2[15:0];
3082
 
3083
                // undocumented behavior: Z flag, see 68knotes.txt
3084
                //sr[2] <= (operand1[15:0] == 16'b0) ? 1'b1 : 1'b0;
3085
                // undocumented behavior: C,V flags, see 68knotes.txt
3086
                //sr[0] <= 1'b0;
3087
                //sr[1] <= 1'b0;
3088
 
3089
                // C,X,V
3090
                //    sr[0] <= (`Sm & ~`Dm) | (`Rm & ~`Dm) | (`Sm & `Rm);
3091
                //    sr[4] <= (`Sm & ~`Dm) | (`Rm & ~`Dm) | (`Sm & `Rm); //=ccr[0];
3092
                //    sr[1] <= (~`Sm & `Dm & ~`Rm) | (`Sm & ~`Dm & `Rm);
3093
                // +: 0-1,    0-0=0, 1-1=0
3094
                // -: 0-0=1,  1-0,   1-1=1
3095
                // operand1 - operand2 > 0
3096
                if( operand1[15:0] != operand2[15:0] && ((~`Dm & `Sm) | (~`Dm & ~`Sm & ~`Rm) | (`Dm & `Sm & ~`Rm)) == 1'b1 ) begin
3097
                    // clear N
3098
                    sr[3] <= 1'b0;
3099 13 alfik
                    alu_signal <= 1'b1;
3100 12 alfik
                end
3101
                // operand1 < 0
3102
                else if( operand1[15] == 1'b1 ) begin
3103
                    // set N
3104
                    sr[3] <= 1'b1;
3105 13 alfik
                    alu_signal <= 1'b1;
3106 12 alfik
                end
3107
                // no trap
3108
                else begin
3109
                    // N undefined: not affected
3110 13 alfik
                    alu_signal <= 1'b0;
3111 12 alfik
                end
3112
 
3113
                // X not affected
3114
            end
3115
 
3116 13 alfik
            `ALU_MULS_MULU_DIVS_DIVU: begin
3117 12 alfik
 
3118
                // division by 0
3119 13 alfik
                if(ir[15:12] == 4'b1000 && operand2[15:0] == 16'b0) begin
3120 12 alfik
                    // X not affected
3121
                    // C cleared
3122
                    sr[0] <= 1'b0;
3123
                    // V,Z,N undefined: cleared
3124
                    sr[1] <= 1'b0;
3125
                    sr[2] <= 1'b0;
3126
                    sr[3] <= 1'b0;
3127
 
3128
                    // set trap
3129 13 alfik
                    alu_signal <= 1'b1;
3130 12 alfik
                end
3131 13 alfik
                // division in idle state
3132
                else if(ir[15:12] == 4'b1000 && div_count == 5'd0) begin
3133
                    alu_signal <= 1'b0;
3134
                end
3135 12 alfik
                // division overflow: divu, divs
3136 13 alfik
                else if(ir[15:12] == 4'b1000 && div_overflow == 1'b1) begin
3137 12 alfik
                    // X not affected
3138
                    // C cleared
3139
                    sr[0] <= 1'b0;
3140
                    // V set
3141
                    sr[1] <= 1'b1;
3142
                    // Z,N undefined: cleared and set
3143
                    sr[2] <= 1'b0;
3144
                    sr[3] <= 1'b1;
3145
 
3146
                    // set trap
3147 13 alfik
                    alu_signal <= 1'b1;
3148 12 alfik
                end
3149
                // division
3150
                else if( ir[15:12] == 4'b1000 ) begin
3151 13 alfik
                    result[31:0] <= {div_remainder, div_quotient};
3152
 
3153 12 alfik
                    // X not affected
3154
                    // C cleared
3155
                    sr[0] <= 1'b0;
3156
                    // V cleared
3157
                    sr[1] <= 1'b0;
3158
                    // Z
3159 13 alfik
                    sr[2] <= (div_quotient == 16'b0);
3160 12 alfik
                    // N
3161 13 alfik
                    sr[3] <= (div_quotient[15] == 1'b1);
3162 12 alfik
 
3163
                    // set trap
3164 13 alfik
                    alu_signal <= 1'b0;
3165 12 alfik
                end
3166
                // multiplication
3167
                else if( ir[15:12] == 4'b1100 ) begin
3168 13 alfik
                    result[31:0] <= mult_result[31:0];
3169 12 alfik
 
3170
                    // X not affected
3171
                    // C cleared
3172
                    sr[0] <= 1'b0;
3173
                    // V cleared
3174
                    sr[1] <= 1'b0;
3175
                    // Z
3176 13 alfik
                    sr[2] <= (mult_result[31:0] == 32'b0);
3177 12 alfik
                    // N
3178 13 alfik
                    sr[3] <= (mult_result[31] == 1'b1);
3179 12 alfik
 
3180
                    // set trap
3181 13 alfik
                    alu_signal <= 1'b0;
3182 12 alfik
                end
3183
            end
3184
 
3185
 
3186
            `ALU_BCHG_BCLR_BSET_BTST: begin // 97 LE
3187
                // byte
3188
                if( ir[5:3] != 3'b000 ) begin
3189
                    sr[2] <= ~(operand1[ operand2[2:0] ]);
3190
                    result = operand1;
3191
                    result[ operand2[2:0] ] = (ir[7:6] == 2'b01) ? ~(operand1[ operand2[2:0] ]) : (ir[7:6] == 2'b10) ? 1'b0 : 1'b1;
3192
                end
3193
                // long
3194
                else if( ir[5:3] == 3'b000 ) begin
3195
                    sr[2] <= ~(operand1[ operand2[4:0] ]);
3196
                    result = operand1;
3197
                    result[ operand2[4:0] ] = (ir[7:6] == 2'b01) ? ~(operand1[ operand2[4:0] ]) : (ir[7:6] == 2'b10) ? 1'b0 : 1'b1;
3198
                end
3199
 
3200
                // C,V,N,X not affected
3201
            end
3202
 
3203
            `ALU_TAS: begin
3204
                result[7:0] <= { 1'b1, operand1[6:0] };
3205
 
3206
                // X not affected
3207
                // C cleared
3208
                sr[0] <= 1'b0;
3209
                // V cleared
3210
                sr[1] <= 1'b0;
3211
 
3212
                // N set
3213
                sr[3] <= (operand1[7] == 1'b1);
3214
                // Z set
3215
                sr[2] <= (operand1[7:0] == 8'b0);
3216
            end
3217
 
3218
 
3219
            `ALU_NEGX_CLR_NEG_NOT_NBCD_SWAP_EXT: begin
3220 13 alfik
                // NEGX / CLR / NEG / NOT
3221
                if ((ir[11:8] == 4'b0000) || (ir[11:8] == 4'b0010) || (ir[11:8] == 4'b0100) || (ir[11:8] == 4'b0110))
3222
                    result = 32'b0 - (operand1[31:0] & {32{ir[10] | ~ir[9]}}) - ((sr[4] & ~ir[10] & ~ir[9]) | (ir[10] & ir[9]));
3223 12 alfik
                // NBCD
3224
                else if( ir[11:6] == 6'b1000_00 ) begin
3225
 
3226
                    result[3:0] = 5'd25 - operand1[3:0];
3227
                    result[7:4] = (operand1[3:0] > 4'd9) ? (5'd24 - operand1[7:4]) : (5'd25 - operand1[7:4]);
3228
 
3229
                    if(sr[4] == 1'b0 && result[3:0] == 4'd9 && result[7:4] == 4'd9) begin
3230
                        result[3:0] = 4'd0;
3231
                        result[7:4] = 4'd0;
3232
                    end
3233
                    else if(sr[4] == 1'b0 && (result[3:0] == 4'd9 || result[3:0] == 4'd15)) begin
3234
                        result[3:0] = 4'd0;
3235
                        result[7:4] = result[7:4] + 4'd1;
3236
                    end
3237
                    else if(sr[4] == 1'b0) begin
3238
                        result[3:0] = result[3:0] + 4'd1;
3239
                    end
3240
 
3241
                    //V undefined: unchanged
3242
                    //Z
3243
                    sr[2] <= sr[2] & `Z;
3244
                    //C,X
3245
                    sr[0] <= (operand1[7:0] == 8'd0 && sr[4] == 1'b0) ? 1'b0 : 1'b1;
3246
                    sr[4] <= (operand1[7:0] == 8'd0 && sr[4] == 1'b0) ? 1'b0 : 1'b1; //=C
3247
                end
3248
                // SWAP
3249
                else if( ir[11:6] == 6'b1000_01 ) result = { operand1[15:0], operand1[31:16] };
3250
                // EXT byte to word
3251
                else if( ir[11:6] == 6'b1000_10 ) result = { result[31:16], {8{operand1[7]}}, operand1[7:0] };
3252
                // EXT word to long
3253
                else if( ir[11:6] == 6'b1000_11 ) result = { {16{operand1[15]}}, operand1[15:0] };
3254
 
3255
                // N set if negative else clear
3256
                sr[3] <= `Rm;
3257
 
3258
                // CLR,NOT,SWAP,EXT
3259
                if( ir[11:8] == 4'b0010 || ir[11:8] == 4'b0110 || ir[11:6] == 6'b1000_01 || ir[11:7] == 5'b1000_1 ) begin
3260
                    // X not affected
3261
                    // C,V cleared
3262
                    sr[0] <= 1'b0;
3263
                    sr[1] <= 1'b0;
3264
                    // Z set
3265
                    sr[2] <= `Z;
3266
                end
3267
                // NEGX
3268
                else if( ir[11:8] == 4'b0000 ) begin
3269
                    // C set if borrow
3270
                    sr[0] <= `Dm | `Rm;
3271
                    // X=C
3272
                    sr[4] <= `Dm | `Rm;
3273
                    // V set if overflow
3274
                    sr[1] <= `Dm & `Rm;
3275
                    // Z cleared if nonzero else unchanged
3276
                    sr[2] <= sr[2] & `Z;
3277
                end
3278
                // NEG
3279
                else if( ir[11:8] == 4'b0100 ) begin
3280
                    // C clear if zero else set
3281
                    sr[0] <= `Dm | `Rm;
3282
                    // X=C
3283
                    sr[4] <= `Dm | `Rm;
3284
                    // V set if overflow
3285
                    sr[1] <= `Dm & `Rm;
3286
                    // Z set if zero else clear
3287
                    sr[2] <= `Z;
3288
                end
3289
            end
3290
 
3291
 
3292
            `ALU_SIMPLE_LONG_ADD: begin
3293
                result <= operand1[31:0] + operand2[31:0];
3294
 
3295
                // CCR not affected
3296
            end
3297
 
3298
            `ALU_SIMPLE_LONG_SUB: begin
3299
                result <= operand1[31:0] - operand2[31:0];
3300
 
3301
                // CCR not affected
3302
            end
3303
 
3304
            `ALU_MOVE_TO_CCR_SR_RTE_RTR_STOP_LOGIC_TO_CCR_SR: begin
3305
 
3306
                // MOVE TO SR,RTE,STOP,ORI to SR,ANDI to SR,EORI to SR
3307
                if( ir[15:8] == 8'b0100_0110 || ir[15:0] == 16'b0100_1110_0111_0011 || ir[15:0] == 16'b0100_1110_0111_0010 ||
3308
                    ir[15:0] == 16'b0000_000_0_01_111100 || ir[15:0] == 16'b0000_001_0_01_111100 || ir[15:0] == 16'b0000_101_0_01_111100
3309
                )         sr <= { operand1[15], 1'b0, operand1[13], 2'b0, operand1[10:8], 3'b0, operand1[4:0] };
3310
                // MOVE TO CCR,RTR,ORI to CCR,ANDI to CCR,EORI to CCR
3311
                else if(     ir[15:8] == 8'b0100_0100 || ir[15:0] == 16'b0100_1110_0111_0111 ||
3312
                            ir[15:0] == 16'b0000_000_0_00_111100 || ir[15:0] == 16'b0000_001_0_00_111100 || ir[15:0] == 16'b0000_101_0_00_111100
3313
                )        sr <= { sr[15:8], 3'b0, operand1[4:0] };
3314
            end
3315
 
3316
            `ALU_SIMPLE_MOVE: begin
3317
                result <= operand1;
3318
 
3319
                // CCR not affected
3320
            end
3321
 
3322
            `ALU_LINK_MOVE: begin
3323
                if(ir[3:0] == 3'b111) begin
3324
                    result <= operand1 - 32'd4;
3325
                end
3326
                else begin
3327
                    result <= operand1;
3328
                end
3329
 
3330
                // CCR not affected
3331
            end
3332
 
3333
        endcase
3334
    end
3335
end
3336
 
3337
endmodule
3338
 
3339
/***********************************************************************************************************************
3340
 * Microcode branch
3341
 **********************************************************************************************************************/
3342
 
3343
/*! \brief Select the next microcode word to execute.
3344
 *
3345
 * The microcode_branch module is responsible for selecting the next microcode word to execute. This decision is based
3346
 * on the value of the current microcode word, the value of the interrupt privilege level, the state of the current
3347
 * bus cycle and other internal signals.
3348
 *
3349
 * The microcode_branch module implements a simple stack for the microcode addresses. This makes it possible to call
3350
 * subroutines inside the microcode.
3351
 */
3352
module microcode_branch(
3353
    input clock,
3354
    input reset_n,
3355
 
3356 13 alfik
    input [4:0]     movem_loop,
3357
    input [15:0]    movem_reg,
3358
    input [31:0]    operand2,
3359
    input           alu_signal,
3360
    input           alu_mult_div_ready,
3361
    input           condition,
3362
    input [31:0]    result,
3363
    input           overflow,
3364
    input           stop_flag,
3365
    input [15:0]    ir,
3366
    input [7:0]     decoder_trap,
3367
    input           trace_flag,
3368
    input           group_0_flag,
3369
    input [2:0]     interrupt_mask,
3370 12 alfik
 
3371 13 alfik
    input [8:0]     load_ea,
3372
    input [8:0]     perform_ea_read,
3373
    input [8:0]     perform_ea_write,
3374
    input [8:0]     save_ea,
3375
    input [8:0]     decoder_micropc,
3376 12 alfik
 
3377 13 alfik
    input           prefetch_ir_valid_32,
3378
    input           prefetch_ir_valid,
3379
    input           jmp_address_trap,
3380
    input           jmp_bus_trap,
3381
    input           finished,
3382 12 alfik
 
3383 13 alfik
    input [3:0]     branch_control,
3384
    input [3:0]     branch_offset,
3385
    output [8:0]    micro_pc
3386 12 alfik
);
3387
 
3388
reg [8:0] micro_pc_0 = 9'd0;
3389
reg [8:0] micro_pc_1;
3390
reg [8:0] micro_pc_2;
3391
reg [8:0] micro_pc_3;
3392
 
3393
assign micro_pc =
3394
    (reset_n == 1'b0) ? 9'd0 :
3395
    (jmp_address_trap == 1'b1 || jmp_bus_trap == 1'b1) ? `MICROPC_ADDRESS_BUS_TRAP :
3396
    (   (branch_control == `BRANCH_movem_loop               && movem_loop == 5'b10000) ||
3397
        (branch_control == `BRANCH_movem_reg                && movem_reg[0] == 0) ||
3398
        (branch_control == `BRANCH_operand2                 && operand2[5:0] == 6'b0) ||
3399 13 alfik
        (branch_control == `BRANCH_alu_signal               && alu_signal == 1'b0) ||
3400
        (branch_control == `BRANCH_alu_mult_div_ready       && alu_mult_div_ready == 1'b1) ||
3401 12 alfik
        (branch_control == `BRANCH_condition_0              && condition == 1'b0) ||
3402
        (branch_control == `BRANCH_condition_1              && condition == 1'b1) ||
3403
        (branch_control == `BRANCH_result                   && result[15:0] == 16'hFFFF) ||
3404
        (branch_control == `BRANCH_V                        && overflow == 1'b0) ||
3405
        (branch_control == `BRANCH_movep_16                 && ir[6] == 1'b0) ||
3406
        (branch_control == `BRANCH_stop_flag_wait_ir_decode && stop_flag == 1'b1) ||
3407
        (branch_control == `BRANCH_ir                       && ir[7:0] != 8'b0) ||
3408
        (branch_control == `BRANCH_trace_flag_and_interrupt && trace_flag == 1'b0 && interrupt_mask != 3'b000) ||
3409
        (branch_control == `BRANCH_group_0_flag             && group_0_flag == 1'b0)
3410
    ) ? micro_pc_0 + { 5'd0, branch_offset } :
3411
    (branch_control == `BRANCH_stop_flag_wait_ir_decode && prefetch_ir_valid == 1'b1 && decoder_trap == 8'd0) ?         decoder_micropc :
3412
    (branch_control == `BRANCH_trace_flag_and_interrupt && trace_flag == 1'b0 && interrupt_mask == 3'b000) ?            `MICROPC_MAIN_LOOP :
3413
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_jump_to_main_loop) ?                            `MICROPC_MAIN_LOOP :
3414
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_call_load_ea && load_ea != 9'd0) ?              load_ea :
3415
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_call_perform_ea_read) ?                         perform_ea_read :
3416
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_call_perform_ea_write) ?                        perform_ea_write :
3417
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_call_save_ea && save_ea != 9'd0) ?              save_ea :
3418
 
3419
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_call_read && load_ea != 9'd0) ?                 load_ea :
3420
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_call_read && load_ea == 9'd0) ?                 perform_ea_read :
3421
 
3422
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_call_write) ?                                   perform_ea_write :
3423
 
3424
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_call_trap) ?                                    `MICROPC_TRAP_ENTRY :
3425
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_return) ?                                       micro_pc_1 :
3426
    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_interrupt_mask && interrupt_mask == 3'b000) ?   `MICROPC_MAIN_LOOP :
3427
    (    (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_wait_finished && finished == 1'b0) ||
3428
        (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_wait_prefetch_valid && prefetch_ir_valid == 1'b0) ||
3429
        (branch_control == `BRANCH_procedure && branch_offset == `PROCEDURE_wait_prefetch_valid_32 && prefetch_ir_valid_32 == 1'b0) ||
3430
        (branch_control == `BRANCH_stop_flag_wait_ir_decode && prefetch_ir_valid == 1'b0)
3431
    ) ? micro_pc_0 :
3432
    micro_pc_0 + 9'd1
3433
;
3434
 
3435
always @(posedge clock or negedge reset_n) begin
3436
    if(reset_n == 1'b0) micro_pc_0 <= 9'd0;
3437
    else                micro_pc_0 <= micro_pc;
3438
end
3439
 
3440
always @(posedge clock or negedge reset_n) begin
3441
    if(reset_n == 1'b0) begin
3442
        micro_pc_1 <= 9'd0;
3443
        micro_pc_2 <= 9'd0;
3444
        micro_pc_3 <= 9'd0;
3445
    end
3446
    else if(branch_control == `BRANCH_stop_flag_wait_ir_decode && prefetch_ir_valid == 1'b1 && decoder_trap == 8'd0)
3447
    begin
3448
        micro_pc_1 <= micro_pc_0 + { 5'd0, branch_offset };
3449
        micro_pc_2 <= micro_pc_1;
3450
        micro_pc_3 <= micro_pc_2;
3451
    end
3452
    else if(branch_control == `BRANCH_procedure) begin
3453
        if(branch_offset == `PROCEDURE_call_read && load_ea != 9'd0) begin
3454
            micro_pc_1 <= perform_ea_read;
3455
            micro_pc_2 <= micro_pc_0 + 9'd1;
3456
            micro_pc_3 <= micro_pc_1;
3457
        end
3458
        else if(branch_offset == `PROCEDURE_call_read && load_ea == 9'd0) begin
3459
            micro_pc_1 <= micro_pc_0 + 9'd1;
3460
            micro_pc_2 <= micro_pc_1;
3461
            micro_pc_3 <= micro_pc_2;
3462
        end
3463
        else if(branch_offset == `PROCEDURE_call_write && save_ea != 9'd0) begin
3464
            micro_pc_1 <= save_ea;
3465
            micro_pc_2 <= micro_pc_1;
3466
            micro_pc_3 <= micro_pc_2;
3467
        end
3468
        else if((branch_offset == `PROCEDURE_call_load_ea && load_ea != 9'd0) ||
3469
                (branch_offset == `PROCEDURE_call_perform_ea_read) ||
3470
                (branch_offset == `PROCEDURE_call_perform_ea_write) ||
3471
                (branch_offset == `PROCEDURE_call_save_ea && save_ea != 9'd0) ||
3472
                (branch_offset == `PROCEDURE_call_trap) )
3473
        begin
3474
            micro_pc_1 <= micro_pc_0 + 9'd1;
3475
            micro_pc_2 <= micro_pc_1;
3476
            micro_pc_3 <= micro_pc_2;
3477
        end
3478
        else if(branch_offset == `PROCEDURE_return) begin
3479
            micro_pc_1 <= micro_pc_2;
3480
            micro_pc_2 <= micro_pc_3;
3481
            micro_pc_3 <= 9'd0;
3482
        end
3483
        else if(branch_offset == `PROCEDURE_push_micropc) begin
3484
            micro_pc_1 <= micro_pc_0;
3485
            micro_pc_2 <= micro_pc_1;
3486
            micro_pc_3 <= micro_pc_2;
3487
        end
3488
        else if(branch_offset == `PROCEDURE_pop_micropc) begin
3489
            micro_pc_1 <= micro_pc_2;
3490
            micro_pc_2 <= micro_pc_3;
3491
            micro_pc_3 <= 9'd0;
3492
        end
3493
    end
3494
end
3495
 
3496
endmodule

powered by: WebSVN 2.1.0

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