Xemu [doxygen]  hyppo 0a42be3a057156924bc1b626a687bd6e27349c45 @ Sat 19 Mar 02:15:11 CET 2022
geos.c
Go to the documentation of this file.
1 /* Part of the Xemu project, please visit: https://github.com/lgblgblgb/xemu
2  Copyright (C)2016-2021 LGB (Gábor Lénárt) <lgblgblgb@gmail.com>
3 
4  This is an odd emulator, emulating a Commodore 64 like machine only for the
5  level needed for a special version of GEOS to be able to run on it.
6  You should have a really special one with own disk drive etc, since there
7  is no hardware support for drive emulation etc, but it's built in the emulator
8  with a kind of CPU trap! The purpose: know GEOS better and slowly replace
9  more and more functions on C/emulator level, so at one point it's possible
10  to write a very own version of GEOS without *any* previously used code in
11  the real GEOS. Then it can be even ported to other architectures GEOS wasn't
12  mean to run ever.
13  ---------------------------------------------------------------------------------
14  One interesting plan: write a GEOS emulator which does not use VIC-II bitmapped
15  screen anymore, but the GEOS functions mean to be targeted a "modern UI toolkit",
16  ie GTK, so a dozens years old (unmodified) GEOS app would be able to run on a PC
17  with modern look and feel, ie anti-aliased fonts, whatever ...
18  ---------------------------------------------------------------------------------
19 
20 This program is free software; you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22 the Free Software Foundation; either version 2 of the License, or
23 (at your option) any later version.
24 
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29 
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
33 
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <string.h>
37 
38 #include "xemu/emutools.h"
39 
40 #include "commodore_geos.h"
41 #include "xemu/cpu65.h"
42 #include "geos.h"
43 #include "xemu/emutools_files.h"
44 
45 
46 
47 
48 void inject_screencoded_message ( int addr, const char *s )
49 {
50  while (*s) {
51  unsigned char c = (unsigned char)(*(s++));
52  if (c >= 65 && c <= 90)
53  c -= 64;
54  else if (c >= 97 && c <= 122)
55  c -= 96;
56  colour_sram[addr] = 0xF1;
57  memory[addr + 0x0400] = c;
58  addr++;
59  }
60 }
61 
62 
63 
64 static int check_basic_stub ( Uint8 *p, int addr )
65 {
66  int a = 10;
67  if (*p != 0x9E)
68  return -1; // should be "SYS" token
69  p++;
70  while (*p == 0x20 && --a)
71  p++;
72  if (!a)
73  return -1;
74  DEBUG("OK! %c%c%c%c" NL, p[0], p[1], p[2], p[3]);
75  if (
76  !isdigit(p[0]) || !isdigit(p[1]) || !isdigit(p[2]) || !isdigit(p[3]) ||
77  isdigit(p[4])
78  )
79  return -1;
80  a = (p[0] - '0') * 1000 + (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
81  if (a < addr + 12 || a > addr + 100)
82  return -1;
83  DEBUG("GEOS: basic stub SYS address is %d decimal." NL, a);
84  return a;
85 }
86 
87 
88 
89 int geos_load_kernal ( const char *kernal_image_name )
90 {
91  Uint8 buffer[0xB000];
92  int addr, len = xemu_load_file(kernal_image_name, buffer, 0x1000, sizeof buffer, "Cannot load specified GEOS kernal image");
93  if (len < 0)
94  return 1;
95  addr = buffer[0] | (buffer[1] << 8); // load address
96  if (addr == 0x5000) {
97  cpu65.pc = 0x5000;
98  } else if (addr == 0x801) {
99  int sys = check_basic_stub(buffer + 6, addr);
100  if (sys < 0) {
101  INFO_WINDOW("Invalid BASIC stub for the GEOS kernal (%s)", xemu_load_filepath);
102  return 1;
103  }
104  cpu65.pc = sys;
105  inject_screencoded_message(6 * 40, "*** Basic stub, you need to wait now ***");
106  } else {
107  INFO_WINDOW("Invalid GEOS kernal load address ($%04X) in (%s)", addr, xemu_load_filepath);
108  return 1;
109  }
110  // OK. Everything seems to be so okey ...
111  memcpy(memory + addr, buffer + 2, len - 2);
112  return 0;
113 }
114 
115 
116 void geos_cpu_trap ( Uint8 opcode )
117 {
118  // Hopefully the trap itself is in RAM ..
119  DEBUG("GEOS: TRAP-SYM: \"%s\"" NL, memory + cpu65.pc);
120  cpu65.pc += strlen((const char*)memory + cpu65.pc) + 1;
121 }
geos_cpu_trap
void geos_cpu_trap(Uint8 opcode)
Definition: geos.c:116
emutools.h
commodore_geos.h
geos_load_kernal
int geos_load_kernal(const char *kernal_image_name)
Definition: geos.c:89
inject_screencoded_message
void inject_screencoded_message(int addr, const char *s)
Definition: geos.c:48
INFO_WINDOW
#define INFO_WINDOW(...)
Definition: xep128.h:114
addr
int addr
Definition: dma65.c:81
Uint8
uint8_t Uint8
Definition: fat32.c:51
emutools_files.h
geos.h
NL
#define NL
Definition: fat32.c:37
memory
Uint8 memory[0x100000]
Definition: commodore_65.c:43
xemu_load_file
int xemu_load_file(const char *filename, void *store_to, int min_size, int max_size, const char *cry)
Definition: emutools_files.c:674
cpu65.h
colour_sram
Uint8 colour_sram[1024]
Definition: commodore_geos.c:117
xemu_load_filepath
char xemu_load_filepath[PATH_MAX]
Definition: emutools_files.c:35
DEBUG
#define DEBUG(...)
Definition: emutools_basicdefs.h:167