Xemu [doxygen]  hyppo 0a42be3a057156924bc1b626a687bd6e27349c45 @ Sat 19 Mar 02:15:11 CET 2022
hardware.c
Go to the documentation of this file.
1 /* Re-CP/M: CP/M-like own implementation + Z80 emulator
2  Part of the Xemu project, please visit: https://github.com/lgblgblgb/xemu
3  Copyright (C)2016-2019 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 "bios.h"
22 #include "bdos.h"
23 #include "console.h"
24 
25 #include <string.h>
26 
28 Uint8 memory[0x10000];
29 Uint8 modded[0x10000]; // purpose: to try to filter if someone tries to read/exec uninitialized code/data
32 
33 int cpu_cycles = 0;
35 int cpu_mhz;
36 int trace;
37 
38 
40 {
41  return memory[addr];
42 }
43 
45 {
46  if (XEMU_UNLIKELY(addr < 8 || addr >= bdos_start)) {
47  FATAL("Tampering system memory addr=$%04X PC=$%04X" NL, addr, Z80_PC);
48  }
49  //if (addr >= bdos_entry_addr)
50 
51  memory[addr] = value;
52  modded[addr] = 1;
53 }
54 
55 void emu_mem_write ( int addr, int data )
56 {
57  if (addr < 0 || addr > 0xFFFF)
58  FATAL("emu_mem_write(): invalid address %d" NL, addr);
59  if (data < 0 || data > 0xFF)
60  FATAL("emu_mem_write(): invalid data %d" NL, data);
61  memory[addr] = data;
62  modded[addr] = 1;
63 }
64 
65 int emu_mem_read ( int addr )
66 {
67  if (addr < 0 || addr > 0xFFFF)
68  FATAL("emu_mem_write(): invalid address %d" NL, addr);
69  return memory[addr];
70 }
71 
72 
73 
75 {
76  return 0xFF;
77 }
78 
80 {
81  int addr = (Z80_PC - 2) & 0xFFFF;
82  //DEBUGPRINT("Z80: I/O OUT write at $%04X" NL, addr);
83  if (bios_handle(addr))
84  goto end;
85  if (bdos_handle(addr))
86  goto end;
87  DEBUGPRINT("Z80-HOOK: unknown I/O operand at $%04X" NL, addr);
88 end:
89  if (emu_cost_cycles) {
91  emu_cost_cycles = 0;
92  }
93  if (emu_cost_usecs) {
95  emu_cost_usecs = 0;
96  }
97  if (stop_emulation) {
98  cpu_cycles += cpu_cycles_per_frame; // to trick emulation loop seeing end of enough cycles emulated ...
99  conputs("\n\rPress SPACE to exit");
100  }
101 }
102 
104 {
105  return 0xFF;
106 }
107 
108 void z80ex_reti_cb ( void )
109 {
110 }
111 
112 
113 static XEMU_INLINE Z80EX_BYTE disasm_mreader ( Z80EX_WORD addr )
114 {
115  return memory[addr & 0xFFFF];
116 }
117 
118 int z80_custom_disasm ( int addr, char *buf, int buf_size )
119 {
120  if (z80ex.prefix) {
121  //snprintf(buf, buf_size, "%04X %02X-PREFIX", addr, z80ex.prefix);
122  *buf = 0;
123  return 0;
124  }
125  int t1, t2;
126  char o_head[256];
127  char o_dasm[256];
128  int oplen = z80ex_dasm(o_dasm, sizeof o_dasm, 0, &t1, &t2, disasm_mreader, addr & 0xFFFF);
129  char *p = strchr(o_dasm, ' ');
130  if (p) {
131  *p++ = '\0';
132  while (*p == ' ')
133  p++;
134  }
135  for (int a = 0; a < oplen; a++)
136  sprintf(o_head + a * 3, "%02X ", disasm_mreader(addr + a));
137  if (p)
138  snprintf(buf, buf_size, "%04X %-12s %-4s %s", addr, o_head, o_dasm, p);
139  else
140  snprintf(buf, buf_size, "%04X %-12s %s", addr, o_head, o_dasm);
141  return oplen;
142 }
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
z80ex_reti_cb
void z80ex_reti_cb(void)
Definition: hardware.c:183
modded
Uint8 modded[0x10000]
Definition: hardware.c:29
emutools.h
bdos.h
z80_custom_disasm
int z80_custom_disasm(int addr, char *buf, int buf_size)
Definition: hardware.c:193
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
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
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
Uint8
uint8_t Uint8
Definition: fat32.c:51
_z80_cpu_context
Definition: z80ex.h:143
bios.h
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
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
hardware.h
Z80_PC
#define Z80_PC
Definition: z80ex.h:121
bdos_handle
int bdos_handle(int addr)
Definition: bdos.c:79
value
int value
Definition: dma65.c:90
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
bdos_start
int bdos_start
Definition: bdos.c:26
conputs
void conputs(const char *s)
Definition: console.c:140
FATAL
#define FATAL(...)
Definition: xep128.h:117
console.h
XEMU_UNLIKELY
#define XEMU_UNLIKELY(__x__)
Definition: emutools_basicdefs.h:125
emu_cost_cycles
int emu_cost_cycles
Definition: hardware.c:30
buf
Uint8 buf[512]
Definition: fat32.c:155