Xemu [doxygen]  hyppo 0a42be3a057156924bc1b626a687bd6e27349c45 @ Sat 19 Mar 02:15:11 CET 2022
hardware.c
Go to the documentation of this file.
1 /* RC2014 and generic Z80 SBC emulator
2  Part of the Xemu project, please visit: https://github.com/lgblgblgb/xemu
3  Copyright (C)2020 LGB (Gábor Lénárt) <lgblgblgb@gmail.com>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18 
19 #include "xemu/emutools.h"
20 #include "hardware.h"
21 #include "console.h"
22 #include "fake_rom.h"
23 #include <string.h>
24 
25 #define OPCODE_RET 0xC9
26 
27 
29 Uint8 memory[0x10000];
31 
32 int cpu_cycles = 0;
34 int trace;
36 
41 };
42 
43 
44 static int internal_rom_is_in_use = 0;
45 
46 
47 static inline void place_ed_trap ( Uint16 addr, enum ed_trap_types trap_no ) {
48  memory[addr] = 0xED;
49  memory[addr+1] = trap_no;
50 }
51 static inline void place_ed_trap_with_ret ( Uint16 addr, enum ed_trap_types trap_no ) {
52  place_ed_trap(addr, trap_no);
53  memory[addr + 2] = OPCODE_RET;
54 }
55 
56 void use_internal_rom ( int yes )
57 {
58  if (yes) {
59  memset(memory, 0x00, 0x8000);
60  place_ed_trap(0, ED_TRAP_RESET);
61  place_ed_trap(2, ED_TRAP_SERVICE_BEGIN);
62  place_ed_trap(4, ED_TRAP_SERVICE_RUN);
63  place_ed_trap_with_ret(0x08, ED_TRAP_RST_08);
64  place_ed_trap_with_ret(0x10, ED_TRAP_RST_10);
65  place_ed_trap_with_ret(0x18, ED_TRAP_RST_18);
66  place_ed_trap_with_ret(0x20, ED_TRAP_RST_20);
67  place_ed_trap_with_ret(0x28, ED_TRAP_RST_28);
68  place_ed_trap_with_ret(0x30, ED_TRAP_RST_30);
69  place_ed_trap_with_ret(0x38, ED_TRAP_RST_38);
70  internal_rom_is_in_use = 1;
71  Z80_PC = 0;
72  Z80_SP = 0xFFFF;
73  } else {
74  internal_rom_is_in_use = 0;
75  }
76 }
77 
78 int z80ex_ed_cb ( Z80EX_BYTE opcode )
79 {
80  Uint16 addr = Z80_PC - 2;
81  if (addr > 0xFF || !internal_rom_is_in_use) {
82  return 1;
83  }
84  //DEBUGPRINT("Z80: trap $%02X @ PC=$%04X" NL, opcode, addr);
85  switch ((enum ed_trap_types)opcode) {
86  case ED_TRAP_RESET:
87  xrcrom_rst(0);
88  break;
90  xrcrom_begin();
91  break;
93  xrcrom_run();
94  break;
95  case ED_TRAP_RST_08:
96  case ED_TRAP_RST_10:
97  case ED_TRAP_RST_18:
98  case ED_TRAP_RST_20:
99  case ED_TRAP_RST_28:
100  case ED_TRAP_RST_30:
101  case ED_TRAP_RST_38:
102  xrcrom_rst(opcode - ED_TRAP_RST_08 + 1);
103  break;
104  case ED_TRAP_QUIT:
105  XEMUEXIT(0);
106  break;
107  default:
108  DEBUGPRINT("ROM: unknown ED trap $%02X at PC $%04X" NL, opcode, addr);
109  return 1;
110  }
111  return 0;
112 }
113 
114 
116 {
117  return memory[addr];
118 }
119 
121 {
122  if (XEMU_UNLIKELY(addr < 0x8000)) {
123  DEBUG("MEM: trying to write ROM at $%04X" NL, addr);
124  return;
125  }
126  memory[addr] = value;
127 }
128 
129 void emu_mem_write ( int addr, int data )
130 {
131  if (addr < 0 || addr > 0xFFFF)
132  FATAL("emu_mem_write(): invalid address %d" NL, addr);
133  if (data < 0 || data > 0xFF)
134  FATAL("emu_mem_write(): invalid data %d" NL, data);
135  memory[addr] = data;
136 }
137 
138 int emu_mem_read ( int addr )
139 {
140  if (addr < 0 || addr > 0xFFFF)
141  FATAL("emu_mem_write(): invalid address %d" NL, addr);
142  return memory[addr];
143 }
144 
145 
146 
148 {
149  return 0xFF;
150 }
151 
153 {
154 #if 0
155  int addr = (Z80_PC - 2) & 0xFFFF;
156  //DEBUGPRINT("Z80: I/O OUT write at $%04X" NL, addr);
157  if (bios_handle(addr))
158  goto end;
159  if (bdos_handle(addr))
160  goto end;
161  DEBUGPRINT("Z80-HOOK: unknown I/O operand at $%04X" NL, addr);
162 end:
163  if (emu_cost_cycles) {
165  emu_cost_cycles = 0;
166  }
167  if (emu_cost_usecs) {
169  emu_cost_usecs = 0;
170  }
171  if (stop_emulation) {
172  cpu_cycles += cpu_cycles_per_frame; // to trick emulation loop seeing end of enough cycles emulated ...
173  conputs("\n\rPress SPACE to exit");
174  }
175 #endif
176 }
177 
179 {
180  return 0xFF;
181 }
182 
183 void z80ex_reti_cb ( void )
184 {
185 }
186 
187 
188 static XEMU_INLINE Z80EX_BYTE disasm_mreader ( Z80EX_WORD addr )
189 {
190  return memory[addr & 0xFFFF];
191 }
192 
193 int z80_custom_disasm ( int addr, char *buf, int buf_size )
194 {
195  if (z80ex.prefix) {
196  //snprintf(buf, buf_size, "%04X %02X-PREFIX", addr, z80ex.prefix);
197  *buf = 0;
198  return 0;
199  }
200  int t1, t2;
201  char o_head[256];
202  char o_dasm[256];
203  int oplen = z80ex_dasm(o_dasm, sizeof o_dasm, 0, &t1, &t2, disasm_mreader, addr & 0xFFFF);
204  char *p = strchr(o_dasm, ' ');
205  if (p) {
206  *p++ = '\0';
207  while (*p == ' ')
208  p++;
209  }
210  for (int a = 0; a < oplen; a++)
211  sprintf(o_head + a * 3, "%02X ", disasm_mreader(addr + a));
212  if (p)
213  snprintf(buf, buf_size, "%04X %-12s %-4s %s", addr, o_head, o_dasm, p);
214  else
215  snprintf(buf, buf_size, "%04X %-12s %s", addr, o_head, o_dasm);
216  return oplen;
217 }
cpu_cycles_per_frame
int cpu_cycles_per_frame
Definition: hardware.c:33
z80ex_mwrite_cb
void z80ex_mwrite_cb(Z80EX_WORD addr, Z80EX_BYTE value)
Definition: hardware.c:120
cpu_cycles
int cpu_cycles
Definition: hardware.c:32
ED_TRAP_RST_38
@ ED_TRAP_RST_38
Definition: hardware.c:39
z80ex_reti_cb
void z80ex_reti_cb(void)
Definition: hardware.c:183
emutools.h
ED_TRAP_RST_30
@ ED_TRAP_RST_30
Definition: hardware.c:39
ED_TRAP_RST_28
@ ED_TRAP_RST_28
Definition: hardware.c:39
fake_rom.h
Z80_SP
#define Z80_SP
Definition: z80ex.h:117
z80_custom_disasm
int z80_custom_disasm(int addr, char *buf, int buf_size)
Definition: hardware.c:193
xrcrom_rst
void xrcrom_rst(int n)
Definition: fake_rom.c:69
z80ex_pread_cb
Z80EX_BYTE z80ex_pread_cb(Z80EX_WORD port16)
Definition: hardware.c:147
emu_cost_usecs
int emu_cost_usecs
Definition: hardware.c:30
bios_handle
int bios_handle(int addr)
Definition: bios.c:53
ED_TRAP_RST_10
@ ED_TRAP_RST_10
Definition: hardware.c:38
ED_TRAP_SERVICE_BEGIN
@ ED_TRAP_SERVICE_BEGIN
Definition: hardware.c:38
use_internal_rom
void use_internal_rom(int yes)
Definition: hardware.c:56
hardware.h
emu_mem_write
void emu_mem_write(int addr, int data)
Definition: hardware.c:129
addr
int addr
Definition: dma65.c:81
XEMU_INLINE
#define XEMU_INLINE
Definition: emutools_basicdefs.h:126
m65-memcontent-generator.data
data
Definition: m65-memcontent-generator.py:119
ED_TRAP_QUIT
@ ED_TRAP_QUIT
Definition: hardware.c:40
Z80EX_WORD
unsigned short Z80EX_WORD
Definition: z80ex.h:51
stop_emulation
int stop_emulation
Definition: hardware.c:30
z80ex_pwrite_cb
void z80ex_pwrite_cb(Z80EX_WORD port16, Z80EX_BYTE value)
Definition: hardware.c:152
z80ex_intread_cb
Z80EX_BYTE z80ex_intread_cb(void)
Definition: hardware.c:178
ED_TRAP_RST_08
@ ED_TRAP_RST_08
Definition: hardware.c:38
Uint8
uint8_t Uint8
Definition: fat32.c:51
xrcrom_run
void xrcrom_run(void)
Definition: fake_rom.c:113
_z80_cpu_context
Definition: z80ex.h:143
z80ex_ed_cb
int z80ex_ed_cb(Z80EX_BYTE opcode)
Definition: hardware.c:78
z80ex_mread_cb
Z80EX_BYTE z80ex_mread_cb(Z80EX_WORD addr, int m1_state)
Definition: hardware.c:115
cpu_mhz
int cpu_mhz
Definition: hardware.c:35
trace
int trace
Definition: hardware.c:34
emu_mem_read
int emu_mem_read(int addr)
Definition: hardware.c:138
DEBUGPRINT
#define DEBUGPRINT(...)
Definition: emutools_basicdefs.h:171
ed_trap_types
ed_trap_types
Definition: hardware.c:37
Z80EX_BYTE
unsigned char Z80EX_BYTE
Definition: z80ex.h:49
z80ex
Z80EX_CONTEXT z80ex
Definition: hardware.c:28
NL
#define NL
Definition: fat32.c:37
_z80_cpu_context::prefix
Z80EX_BYTE prefix
Definition: z80ex.h:163
memory
Uint8 memory[0x10000]
Definition: hardware.c:29
XEMUEXIT
#define XEMUEXIT(n)
Definition: emutools_basicdefs.h:246
io_cycles
int io_cycles
Definition: hardware.c:35
OPCODE_RET
#define OPCODE_RET
Definition: hardware.c:25
Z80_PC
#define Z80_PC
Definition: z80ex.h:121
bdos_handle
int bdos_handle(int addr)
Definition: bdos.c:79
ED_TRAP_RST_18
@ ED_TRAP_RST_18
Definition: hardware.c:39
console.h
value
int value
Definition: dma65.c:90
ED_TRAP_RST_20
@ ED_TRAP_RST_20
Definition: hardware.c:39
Uint16
uint16_t Uint16
Definition: fat32.c:50
xrcrom_begin
void xrcrom_begin(void)
Definition: fake_rom.c:106
z80ex_dasm
int z80ex_dasm(char *output, int output_size, unsigned flags, int *t_states, int *t_states2, z80ex_dasm_readbyte_cb readbyte_cb, Z80EX_WORD addr)
Definition: z80ex_dasm.c:42
DEBUG
#define DEBUG(...)
Definition: emutools_basicdefs.h:167
conputs
void conputs(const char *s)
Definition: console.c:140
FATAL
#define FATAL(...)
Definition: xep128.h:117
ED_TRAP_RESET
@ ED_TRAP_RESET
Definition: hardware.c:38
XEMU_UNLIKELY
#define XEMU_UNLIKELY(__x__)
Definition: emutools_basicdefs.h:125
ED_TRAP_SERVICE_RUN
@ ED_TRAP_SERVICE_RUN
Definition: hardware.c:38
emu_cost_cycles
int emu_cost_cycles
Definition: hardware.c:30
buf
Uint8 buf[512]
Definition: fat32.c:155