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

Subversion Repositories aoocs

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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