llvm-mos-sdk
cbm.h
Go to the documentation of this file.
1 // Copyright 2022 LLVM-MOS Project
2 // Licensed under the Apache License, Version 2.0 with LLVM Exceptions.
3 // See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license
4 // information.
5 
6 // Originally from cc65. Modified from original version.
7 
8 // clang-format off
9 
10 /*****************************************************************************/
11 /* */
12 /* cbm.h */
13 /* */
14 /* CBM system-specific definitions */
15 /* */
16 /* */
17 /* */
18 /* (C) 1998-2015, Ullrich von Bassewitz */
19 /* Roemerstrasse 52 */
20 /* D-70794 Filderstadt */
21 /* EMail: uz@cc65.org */
22 /* */
23 /* */
24 /* This software is provided 'as-is', without any expressed or implied */
25 /* warranty. In no event will the authors be held liable for any damages */
26 /* arising from the use of this software. */
27 /* */
28 /* Permission is granted to anyone to use this software for any purpose, */
29 /* including commercial applications, and to alter it and redistribute it */
30 /* freely, subject to the following restrictions: */
31 /* */
32 /* 1. The origin of this software must not be misrepresented; you must not */
33 /* claim that you wrote the original software. If you use this software */
34 /* in a product, an acknowledgment in the product documentation would be */
35 /* appreciated but is not required. */
36 /* 2. Altered source versions must be plainly marked as such, and must not */
37 /* be misrepresented as being the original software. */
38 /* 3. This notice may not be removed or altered from any source */
39 /* distribution. */
40 /* */
41 /*****************************************************************************/
42 
43 
44 
45 #ifndef _CBM_H
46 #define _CBM_H
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 
53 /* Check for errors */
54 #if !defined(__CBM__)
55 # error This module may be used only when compiling for CBM machines!
56 #endif
57 
58 
59 
60 /* We need NULL. */
61 #include <stddef.h>
62 
63 /* Load the system-specific files here, if needed. */
64 #if defined(__C64__) && !defined(_C64_H)
65 # include <c64.h>
66 #elif defined(__VIC20__) && !defined(_VIC20_H)
67 # include <vic20.h>
68 #elif defined(__C128__) && !defined(_C128_H)
69 # include <c128.h>
70 #elif defined(__PLUS4__) && !defined(_PLUS4_H)
71 # include <plus4.h>
72 #elif defined(__C16__) && !defined(_C16_H)
73 # include <c16.h>
74 #elif defined(__CBM510__) && !defined(_CBM510_H)
75 # include <cbm510.h>
76 #elif defined(__CBM610__) && !defined(_CBM610_H)
77 # include <cbm610.h>
78 #elif defined(__PET__) && !defined(_PET_H)
79 # include <pet.h>
80 #elif defined(__CX16__) && !defined(_CX16_H)
81 # include <cx16.h>
82 #endif
83 
84 // TODO
85 #if 0
86 /* Include definitions for CBM file types */
87 #include <cbm_filetype.h>
88 #endif
89 
90 
91 
92 #define JOY_FIRE_MASK JOY_BTN_1_MASK
93 #define JOY_FIRE(v) ((v) & JOY_FIRE_MASK)
94 
95 
96 
97 /*****************************************************************************/
98 /* Variables */
99 /*****************************************************************************/
100 
101 
102 
103 /* The file stream implementation and the POSIX I/O functions will
104 ** use the following variable to determine the file type to use.
105 */
106 // TODO
107 //extern char _filetype; /* Defaults to 's' */
108 
109 
110 
111 /*****************************************************************************/
112 /* Character-codes (CBM charset) */
113 /*****************************************************************************/
114 
115 
116 
117 #define CH_HLINE 192
118 #define CH_VLINE 221
119 #define CH_ULCORNER 176
120 #define CH_URCORNER 174
121 #define CH_LLCORNER 173
122 #define CH_LRCORNER 189
123 #define CH_TTEE 178
124 #define CH_BTEE 177
125 #define CH_LTEE 171
126 #define CH_RTEE 179
127 #define CH_CROSS 219
128 #define CH_CURS_UP 145
129 #define CH_CURS_DOWN 17
130 #define CH_CURS_LEFT 157
131 #define CH_CURS_RIGHT 29
132 #define CH_PI 222
133 #define CH_HOME 19
134 #define CH_DEL 20
135 #define CH_INS 148
136 #define CH_ENTER 13
137 #define CH_STOP 3
138 #define CH_LIRA 92
139 #define CH_ESC 27
140 #define CH_FONT_LOWER 14
141 #define CH_FONT_UPPER 142
142 
143 
144 
145 /*****************************************************************************/
146 /* Definitions for directory reading functions */
147 /*****************************************************************************/
148 
149 
150 
151 /* CBM FILE ACCESS */
152 #define CBM_A_RO 1 /* Read only */
153 #define CBM_A_WO 2 /* Write only */
154 #define CBM_A_RW 3 /* Read, Write */
155 
156 struct cbm_dirent {
157  char name[17]; /* File name in PetSCII, limited to 16 chars */
158  unsigned int size; /* Size, in 254-/256-byte blocks */
159  unsigned char type;
160  unsigned char access;
161 };
162 
163 
164 
165 /*****************************************************************************/
166 /* Machine info */
167 /*****************************************************************************/
168 
169 
170 // TODO
171 #if 0
172 
173 #define TV_NTSC 0
174 #define TV_PAL 1
175 #define TV_OTHER 2
176 
177 unsigned char get_tv (void);
178 /* Return the video mode the machine is using. */
179 
180 #define KBREPEAT_CURSOR 0x00
181 #define KBREPEAT_NONE 0x40
182 #define KBREPEAT_ALL 0x80
183 
184 unsigned char __fastcall__ kbrepeat (unsigned char mode);
185 /* Changes which keys have automatic repeat. */
186 
187 #if !defined(__CBM610__)
188 void waitvsync (void);
189 /* Wait for the start of the next video field. */
190 #endif
191 
192 #endif
193 
194 /*****************************************************************************/
195 /* CBM kernal functions */
196 /*****************************************************************************/
197 
198 /* Constants to use with cbm_open() for openning a file for reading or
199 ** writing without the need to append ",r" or ",w" to the filename.
200 **
201 ** e.g., cbm_open(2, 8, CBM_READ, "0:data,s");
202 */
203 #define CBM_READ 0 /* default is ",p" */
204 #define CBM_WRITE 1 /* ditto */
205 #define CBM_SEQ 2 /* default is ",r" -- or ",s" when writing */
206 
208 enum : unsigned char {
211 #ifdef __CX16__
212  CX16_LOAD_VRAM0 = 2,
213  CX16_LOAD_VRAM1 = 3,
214 #endif
215 };
216 
217 /* Kernal-level functions */
218 unsigned char cbm_k_acptr (void);
219 unsigned char cbm_k_basin (void);
220 void cbm_k_bsout (unsigned char C);
221 unsigned char cbm_k_chkin (unsigned char FN) __attribute__((leaf));
222 unsigned char cbm_k_chkout (unsigned char FN) __attribute__((leaf));
223 unsigned char cbm_k_chrin (void);
224 void cbm_k_chrout (unsigned char C);
225 void cbm_k_cint (void);
226 void cbm_k_ciout (unsigned char C);
227 unsigned char cbm_k_ckout (unsigned char FN) __attribute__((leaf));
228 void cbm_k_clall (void);
229 void cbm_k_close (unsigned char FN);
230 void cbm_k_clrch (void);
231 unsigned char cbm_k_getin (void);
232 unsigned cbm_k_iobase (void) __attribute__((leaf));
233 void cbm_k_listen (unsigned char dev);
234 
255 void *cbm_k_load(unsigned char flag, void *load_addr);
256 
257 unsigned char cbm_k_open (void) __attribute__((leaf));
258 unsigned char cbm_k_readst (void);
259 unsigned char cbm_k_save(void *startaddr, void *endaddr_plusone) __attribute__((leaf));
260 void cbm_k_scnkey (void);
261 void cbm_k_second (unsigned char addr);
262 void cbm_k_setlfs (unsigned char LFN, unsigned char DEV,
263  unsigned char SA) __attribute__((leaf));
264 void cbm_k_setnam (const char* Name) __attribute__((leaf));
265 void cbm_k_settim (unsigned long timer);
266 void cbm_k_talk (unsigned char dev);
267 void cbm_k_tksa (unsigned char addr);
268 void cbm_k_udtim (void);
269 void cbm_k_unlsn (void);
270 void cbm_k_untlk (void);
271 
272 
273 /*****************************************************************************/
274 /* BASIC-like file I/O functions */
275 /*****************************************************************************/
276 
277 /* The cbm_* I/O functions below set _oserror (see errno.h),
278 ** in case of an error.
279 **
280 ** error-code BASIC error
281 ** ---------- -----------
282 ** 1 = too many files
283 ** 2 = file open
284 ** 3 = file not open
285 ** 4 = file not found
286 ** 5 = device not present
287 ** 6 = not input-file
288 ** 7 = not output-file
289 ** 8 = missing file-name
290 ** 9 = illegal device-number
291 **
292 ** 10 = STOP-key pushed
293 ** 11 = general I/O-error
294 */
295 
296 
297 // TODO
298 #if 0
299 
300 unsigned int __fastcall__ cbm_load (const char* name, unsigned char device, void* data);
301 /* Loads file "name", from given device, to given address -- or, to the load
302 ** address of the file if "data" is the null pointer (like load"name",8,1
303 ** in BASIC).
304 ** Returns number of bytes that were loaded if loading was successful;
305 ** otherwise 0, "_oserror" contains an error-code, then (see table above).
306 */
307 
308 unsigned char __fastcall__ cbm_save (const char* name, unsigned char device,
309  const void* addr, unsigned int size);
310 /* Saves "size" bytes, starting at "addr", to a file.
311 ** Returns 0 if saving was successful, otherwise an error-code (see table
312 ** above).
313 */
314 
315 unsigned char __fastcall__ cbm_open (unsigned char lfn, unsigned char device,
316  unsigned char sec_addr, const char* name);
317 /* Opens a file. Works just like the BASIC command.
318 ** Returns 0 if openning was successful, otherwise an error-code (see table
319 ** above).
320 */
321 
322 void __fastcall__ cbm_close (unsigned char lfn);
323 /* Closes a file */
324 
325 int __fastcall__ cbm_read (unsigned char lfn, void* buffer, unsigned int size);
326 /* Reads up to "size" bytes from a file into "buffer".
327 ** Returns the number of actually-read bytes, 0 if there are no bytes left.
328 ** -1 in case of an error; then, _oserror contains an error-code (see table
329 ** above). (Remember: 0 means end-of-file; -1 means error.)
330 */
331 
332 int __fastcall__ cbm_write (unsigned char lfn, const void* buffer,
333  unsigned int size);
334 /* Writes up to "size" bytes from "buffer" to a file.
335 ** Returns the number of actually-written bytes, or -1 in case of an error;
336 ** _oserror contains an error-code, then (see above table).
337 */
338 
339 unsigned char cbm_opendir (unsigned char lfn, unsigned char device, ...);
340 /* Opens directory listing. Returns 0 if opening directory was successful;
341 ** otherwise, an error-code corresponding to cbm_open(). As an optional
342 ** argument, the name of the directory may be passed to the function. If
343 ** no explicit name is specified, "$" is used.
344 */
345 
346 unsigned char __fastcall__ cbm_readdir (unsigned char lfn,
347  struct cbm_dirent* l_dirent);
348 /* Reads one directory line into cbm_dirent structure.
349 ** Returns 0 if reading directory-line was successful.
350 ** Returns non-zero if reading directory failed, or no more file-names to read.
351 ** Returns 2 on last line. Then, l_dirent->size = the number of "blocks free",
352 ** "blocks used", or "mb free". Return codes:
353 ** 0 = read file-name
354 ** 1 = couldn't read directory
355 ** 2 = read "blocks free", "blocks used", or "mb free"
356 ** 3 = couldn't find start of file-name
357 ** 4 = couldn't find end of file-name
358 ** 5 = couldn't read file-type
359 ** 6 = premature end of file
360 */
361 
362 void __fastcall__ cbm_closedir (unsigned char lfn);
363 /* Closes directory by cbm_close(lfn) */
364 
365 #endif
366 
367 #ifdef __cplusplus
368 }
369 #endif
370 
371 /* End of cbm.h */
372 #endif
cbm_dirent::type
unsigned char type
Definition: cbm.h:159
cbm_k_bsout
void cbm_k_bsout(unsigned char C)
cbm_k_chrout
void cbm_k_chrout(unsigned char C)
cbm_dirent::size
unsigned int size
Definition: cbm.h:158
data
char const void * data
Definition: neslib.h:92
c128.h
cbm_k_tksa
void cbm_k_tksa(unsigned char addr)
c64.h
cbm_k_save
unsigned char cbm_k_save(void *startaddr, void *endaddr_plusone) __attribute__((leaf))
cbm_k_udtim
void cbm_k_udtim(void)
cbm_dirent
Definition: cbm.h:156
cbm_k_basin
unsigned char cbm_k_basin(void)
cbm_k_setlfs
void cbm_k_setlfs(unsigned char LFN, unsigned char DEV, unsigned char SA) __attribute__((leaf))
cbm_k_chrin
unsigned char cbm_k_chrin(void)
cbm_k_ckout
unsigned char cbm_k_ckout(unsigned char FN) __attribute__((leaf))
cbm_k_load
void * cbm_k_load(unsigned char flag, void *load_addr)
CBM KERNAL function to load or verify file.
get_tv
unsigned char get_tv(void) __attribute__((leaf))
cbm_k_listen
void cbm_k_listen(unsigned char dev)
cbm_k_second
void cbm_k_second(unsigned char addr)
cbm_k_scnkey
void cbm_k_scnkey(void)
CBM_LOAD_RAM
@ CBM_LOAD_RAM
Load into RAM.
Definition: cbm.h:209
cbm_k_chkout
unsigned char cbm_k_chkout(unsigned char FN) __attribute__((leaf))
cbm_k_untlk
void cbm_k_untlk(void)
size
unsigned size
Definition: neslib.h:185
cx16.h
vic20.h
cbm_k_acptr
unsigned char cbm_k_acptr(void)
cbm_dirent::access
unsigned char access
Definition: cbm.h:160
cbm_k_settim
void cbm_k_settim(unsigned long timer)
cbm_k_clrch
void cbm_k_clrch(void)
cbm_k_clall
void cbm_k_clall(void)
cbm_k_unlsn
void cbm_k_unlsn(void)
cbm_k_close
void cbm_k_close(unsigned char FN)
cbm_k_ciout
void cbm_k_ciout(unsigned char C)
waitvsync
void waitvsync(void)
cbm_k_iobase
unsigned cbm_k_iobase(void) __attribute__((leaf))
cbm_k_getin
unsigned char cbm_k_getin(void)
cbm_k_talk
void cbm_k_talk(unsigned char dev)
CBM_LOAD_VERIFY
@ CBM_LOAD_VERIFY
Performs verify.
Definition: cbm.h:210
cbm_k_open
unsigned char cbm_k_open(void) __attribute__((leaf))
cbm_k_setnam
void cbm_k_setnam(const char *Name) __attribute__((leaf))
cbm_k_chkin
unsigned char cbm_k_chkin(unsigned char FN) __attribute__((leaf))
cbm_k_readst
unsigned char cbm_k_readst(void)
mode
const void uint16_t uint8_t mode
Definition: memory.h:58
cbm_dirent::name
char name[17]
Definition: cbm.h:157
cbm_k_cint
void cbm_k_cint(void)
pet.h