Xemu [doxygen]  hyppo 0a42be3a057156924bc1b626a687bd6e27349c45 @ Sat 19 Mar 02:15:11 CET 2022
bdos.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 "bdos.h"
21 #include "hardware.h"
22 #include "console.h"
23 #include "cpmfs.h"
24 
25 
27 int cpm_dma;
28 
29 
30 // Addr must be 256 byte aligned! This is not a need by the func too much, but in general CP/M apps
31 // may depend on that (?).
32 // ALSO, this must be BELOW the BIOS, since this is the address also which is the FIRST allocated
33 // memory byte, cannot be used by a program!
34 void bdos_install ( int addr )
35 {
36  bdos_start = addr;
37  // the dispatch for BDOS entry
38  emu_mem_write(bdos_start + 0, 0xD3); // opcode of "OUT (n),A", we use this as only a dispatch point
39  emu_mem_write(bdos_start + 1, 0xC9); // argument of the OUT, let's use the same as opcode as RET, just for fun, never mind
40  emu_mem_write(bdos_start + 2, 0xC9); // opcode of "RET"
41  // buffered console input ("line editor") dispatch point
42  emu_mem_write(bdos_start + 3, 0xD3);
43  emu_mem_write(bdos_start + 4, 0xC9);
44  emu_mem_write(bdos_start + 5, 0xC9);
45  // write BDOS call to the "zero page"
46  emu_mem_write(5, 0xC3); // opcode of "JP"
47  emu_mem_write(6, bdos_start & 0xFF); // low byte for BDOS entry
48  emu_mem_write(7, bdos_start >> 8); // high byte for BDOS entry
49  DEBUGPRINT("BDOS: installed from $%04X" NL, bdos_start);
50 }
51 
52 
53 static void buffered_console_input_editor ( void )
54 {
55  Uint8 c = console_input();
56 }
57 
58 int bdos_find_first ( const char *dirpath, int fcb_address )
59 {
60  //validate_memory_access(fcb_address, FCB_SIZE);
61  //validate_memory_access(cpm_dma, 128);
63  return 0xFF;
64  } else {
65  int ret = cpmfs_search_file();
66  return (ret >= 0 && ret <= 3) ? ret : 0xFF;
67  }
68 }
69 
70 
71 int bdos_find_next ( void )
72 {
73  int ret = cpmfs_search_file();
74  return (ret >= 0 && ret <= 3) ? ret : 0xFF;
75 }
76 
77 
78 // dispatch addr (OUT emulation with the PC ...)
79 int bdos_handle ( int addr )
80 {
81  if (addr == bdos_start + 3) { // our buffered console input ("line editor") thinggy ...
82  buffered_console_input_editor();
83  Z80_PC -= 2; // re-execute
84  return 1; // it IS handled!
85  }
86  if (addr != bdos_start)
87  return 0; // not BDOS call dispatch single point
88  DEBUG("BDOS: calling function #%d" NL, Z80_C);
89  switch (Z80_C) {
90  case 0: // P_TERMCPM, system reset
91  Z80_PC = 0; // no idea, let continue with BIOS WBOOT ;-P
92  break;
93  case 1: // C_READ, console input, wait for character, also echo it
94  Z80_A = console_input();
95  if (Z80_A == 0)
96  Z80_PC -= 2; // re-execute for waiting ...
97  else {
98  Z80_L = Z80_A;
100  }
101  break;
102  case 2: // C_WRITE - console output
104  break;
105  case 6: // C_RAWIO
106  if (Z80_E == 0xFF) {
107  Z80_A = console_input();
108  Z80_L = Z80_A; // ????
109  } else
111  break;
112  case 9: // C_WRITESTR, send string (@ DE) to the console
113  while (emu_mem_read(Z80_DE) != '$')
115  break;
116  case 11: // C_STAT, console status
117  Z80_A = console_status();
118  Z80_L = Z80_A;
119  break;
120  case 12: // S_BDOSVER, version
121  Z80_B = 0; Z80_H = 0; // system type
122  Z80_A = 0x22; Z80_L = 0x22; // CP/M version, 2.2 here
123  break;
124  case 17: // F_SFIRST - find first
125  DEBUGPRINT("FIXME F_SFIRST [FIRST_BYTE=$%02X]: ", emu_mem_read(Z80_DE));
126  for (int a = 1; a < 12; a++)
127  DEBUGPRINT("%c", emu_mem_read(Z80_DE + a));
128  DEBUGPRINT(NL);
129  Z80_A = Z80_L = bdos_find_first(".", Z80_DE);
130  break;
131  case 18: // F_SNEXT - find next
132  DEBUGPRINT("FIXME F_SNEXT" NL);
133  Z80_A = Z80_L = bdos_find_next();
134  break;
135  case 25: // DRV_GET, get current drive
136  Z80_A = 0; // TODO now we support only drive A:
137  break;
138  case 26: // F_DMAOFF, set DMA address
139  cpm_dma = Z80_DE; // FIXME: check DMA
140  DEBUGPRINT("BDOS: setting DMA to $%04X" NL, cpm_dma);
141  break;
142  default:
143  DEBUGPRINT("BDOS: sorry, function #%d is not implemented" NL, Z80_C);
144  Z80_A = 0; // ??? FIXME: on unknown function ...
145  break;
146  }
147  return 1; // it WAS the BDOS call dispatch!
148 }
Z80_DE
#define Z80_DE
Definition: z80ex.h:85
console_input
int console_input(void)
Definition: console.c:157
console_status
int console_status(void)
Definition: console.c:148
Z80_A
#define Z80_A
Definition: z80ex.h:75
bdos_install
void bdos_install(int addr)
Definition: bdos.c:34
cpmfs.h
emutools.h
bdos.h
cpm_dma
int cpm_dma
Definition: bdos.c:27
emu_mem_write
void emu_mem_write(int addr, int data)
Definition: hardware.c:129
addr
int addr
Definition: dma65.c:81
bdos_find_first
int bdos_find_first(const char *dirpath, int fcb_address)
Definition: bdos.c:58
Z80_L
#define Z80_L
Definition: z80ex.h:88
Uint8
uint8_t Uint8
Definition: fat32.c:51
bdos_find_next
int bdos_find_next(void)
Definition: bdos.c:71
emu_mem_read
int emu_mem_read(int addr)
Definition: hardware.c:138
DEBUGPRINT
#define DEBUGPRINT(...)
Definition: emutools_basicdefs.h:171
CPMFS_SF_JOKERY
#define CPMFS_SF_JOKERY
Definition: cpmfs.h:29
cpmfs_search_file
int cpmfs_search_file(void)
Definition: cpmfs.c:219
Z80_H
#define Z80_H
Definition: z80ex.h:87
NL
#define NL
Definition: fat32.c:37
Z80_E
#define Z80_E
Definition: z80ex.h:84
memory
Uint8 memory[0x100000]
Definition: commodore_65.c:43
Z80_C
#define Z80_C
Definition: z80ex.h:80
hardware.h
Z80_PC
#define Z80_PC
Definition: z80ex.h:121
bdos_handle
int bdos_handle(int addr)
Definition: bdos.c:79
Z80_B
#define Z80_B
Definition: z80ex.h:79
cpmfs_search_file_setup
int cpmfs_search_file_setup(int drive, const Uint8 *input, int options)
Definition: cpmfs.c:285
CPMFS_SF_STORE_IN_DMA0
#define CPMFS_SF_STORE_IN_DMA0
Definition: cpmfs.h:24
CPMFS_SF_INPUT_IS_FCB
#define CPMFS_SF_INPUT_IS_FCB
Definition: cpmfs.h:30
DEBUG
#define DEBUG(...)
Definition: emutools_basicdefs.h:167
bdos_start
int bdos_start
Definition: bdos.c:26
console.h
console_output
void console_output(Uint8 data)
Definition: console.c:106