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

Subversion Repositories ao68000

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

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

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

powered by: WebSVN 2.1.0

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