Skip to content

Commit 1038ea0

Browse files
authored
Update our sample code (#224)
Removed a bunch of old examples that are not so useful, and tidied up the remaining ones: * Removed unused bytes and labels. * Updated to ensure that several reported upon the correct drive-letter. * Little tweaks to make behaviour better. * e.g. Testing that creating a file worked, or ensuring a file exists before trying to delete it. This closes #222.
1 parent 81bb2c5 commit 1038ea0

30 files changed

+170
-400
lines changed

samples/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# The files we wish to generate.
44
#
55
all: cli-args.com create.com \
6-
delete.com drive.com filesize.com find.com \
7-
intest.com read.com ret.com terminal.com test.com \
8-
unimpl.com user-num.com version.com write.com
6+
delete.com filesize.com find.com \
7+
intest.com read.com ret.com tsize.com \
8+
write.com
99

1010

1111
#

samples/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ More significant programs are available within the sister-repository:
2121
* Create a file, given the name.
2222
* [delete.z80](delete.z80)
2323
* Delete files matching the given name/pattern.
24+
* [filesize.z80](filesize.z80)
25+
* Show the size of the given file.
26+
* [find.z80](find.z80)
27+
* Find files via their names.
2428
* [intest.z80](intest.z80)
2529
* Test the various character/line input methods for correctness.
2630
* [read.z80](read.z80) & [write.z80](write.z80)
@@ -29,7 +33,5 @@ More significant programs are available within the sister-repository:
2933
* Used to test sequential read/write operations.
3034
* [ret.z80](ret.z80)
3135
* Terminate the execution of a binary in four different ways.
32-
* [terminal.z80](terminal.z80)
36+
* [tsize.z80](tsize.z80)
3337
* Show the dimensions of the terminal we're running within.
34-
* [unimpl.z80](unimpl.z80)
35-
* Attempt to call a BDOS function which isn't implemented.

samples/cli-args.com

10 Bytes
Binary file not shown.

samples/cli-args.z80

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,29 @@ DUMP_FCB:
6565
PUSH HL
6666
LD A, (HL)
6767

68-
; A should have the drive number 0 means A, 1 for B, etc
69-
add a,'A'
68+
;; If the drive is not-zero that means we have an explicit drive
69+
cp 0x00
70+
jr nz, letter_drive
7071

71-
; Show drive
72+
;; So the drive is 0x00, which means we're using the current,
73+
;; or default, drive. Find it.
74+
ld c,25
75+
call 0x0005
76+
77+
;; Add one, now we can fall-through to the ASCII conversiion
78+
inc a
79+
80+
letter_drive:
81+
; 1 means A, 2 for B, etc
82+
add a,'A' -1
83+
84+
show_drive:
85+
;; Show the drive letter
7286
LD E,A
7387
LD C,0x02
7488
CALL 0x0005
7589

76-
; Show ":"
90+
;; And the ":"
7791
LD E,':'
7892
LD C,0x02
7993
CALL 0x0005

samples/create.com

122 Bytes
Binary file not shown.

samples/create.z80

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
;; create.asm - Create the named file
2-
;;
32

43
FCB1: EQU 0x5C
54

6-
BDOS_ENTRY_POINT: EQU 5
7-
8-
BDOS_OUTPUT_STRING: EQU 9
9-
BDOS_MAKE_FILE: EQU 22
5+
BDOS_ENTRY_POINT: EQU 5
6+
BDOS_OUTPUT_STRING: EQU 9
7+
BDOS_OPEN_FILE: EQU 15
8+
BDOS_MAKE_FILE: EQU 22
109

1110
;;
1211
;; CP/M programs start at 0x100.
@@ -29,24 +28,58 @@ BDOS_MAKE_FILE: EQU 22
2928
jr exit_fn
3029

3130
got_argument:
32-
LD DE, FCB1
33-
LD C, 22
34-
CALL BDOS_ENTRY_POINT
31+
;; First of all try to open the file
32+
;; if this succeeds it means the file
33+
;; exists, so we cannot create it, and we
34+
;; must terminate.
35+
call can_open
36+
jr z, already_present
37+
38+
;; Now try to create the file.
39+
ld de, FCB1
40+
ld c, BDOS_MAKE_FILE
41+
call BDOS_ENTRY_POINT
42+
43+
;; If we can no open it then we created the
44+
;; file, and all is good.
45+
call can_open
46+
jr nz, failed_create
3547

3648
exit_fn:
3749
;; exit
38-
LD C,0x00
39-
CALL BDOS_ENTRY_POINT
50+
ld c,0x00
51+
call BDOS_ENTRY_POINT
4052

4153

42-
;;;
43-
;;; The message displayed if no command-line argument was present.
44-
;;;
45-
usage_message:
46-
db "Usage: CREATE FILENAME.EXT"
54+
;; Test if we can open the file in the first FCB
55+
can_open:
56+
;; Open the file
57+
ld de, FCB1
58+
ld c, BDOS_OPEN_FILE
59+
call BDOS_ENTRY_POINT
60+
61+
;; Did that succeed?
62+
cp 00
63+
ret
4764

48-
;; note fall-through here :)
49-
newline:
50-
db 0xa, 0xd, "$"
65+
already_present:
66+
ld de, present_message
67+
ld c, BDOS_OUTPUT_STRING
68+
call BDOS_ENTRY_POINT
69+
jr exit_fn
70+
71+
failed_create:
72+
ld de, failed_message
73+
ld c, BDOS_OUTPUT_STRING
74+
call BDOS_ENTRY_POINT
75+
jr exit_fn
76+
77+
;;; Message area
78+
failed_message:
79+
DB "Failed to create the file.", 0x0a, 0x0d, "$"
80+
present_message:
81+
DB "The file is already present, we cannot create it.", 0x0a, 0x0d, "$"
82+
usage_message:
83+
db "Usage: CREATE FILENAME.EXT", 0xa, 0xd, "$"
5184

5285
END

samples/delete.com

101 Bytes
Binary file not shown.

samples/delete.z80

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ FCB1: EQU 0x5C
55

66
BDOS_ENTRY_POINT: EQU 5
77

8-
BDOS_OUTPUT_STRING: EQU 9
9-
BDOS_DELETE_FILE: EQU 19
8+
BDOS_OUTPUT_STRING: EQU 9
9+
BDOS_OPEN_FILE: EQU 15
10+
BDOS_DELETE_FILE: EQU 19
1011

1112
;;
1213
;; CP/M programs start at 0x100.
@@ -29,24 +30,56 @@ BDOS_DELETE_FILE: EQU 19
2930
jr exit_fn
3031

3132
got_argument:
33+
;; Can we open the file?
34+
;; i.e. if it doesn't exist we must abort
35+
call can_open
36+
jr nz, not_found
37+
38+
;; file exists, we can delete it.
3239
LD DE, FCB1
3340
LD C, BDOS_DELETE_FILE
3441
CALL BDOS_ENTRY_POINT
3542

43+
;; did it work?
44+
call can_open
45+
jr z, delete_failed
46+
3647
exit_fn:
3748
;; exit
3849
LD C,0x00
3950
CALL BDOS_ENTRY_POINT
4051

52+
not_found:
53+
ld de, NOT_FOUND_MESSAGE
54+
ld c, BDOS_OUTPUT_STRING
55+
call BDOS_ENTRY_POINT
56+
jr exit_fn
57+
58+
delete_failed:
59+
ld de, DELETE_FAILED_MESSAGE
60+
ld c, BDOS_OUTPUT_STRING
61+
call BDOS_ENTRY_POINT
62+
jr exit_fn
63+
64+
;; Test if we can open the file in the first FCB
65+
can_open:
66+
;; Open the file
67+
ld de, FCB1
68+
ld c, BDOS_OPEN_FILE
69+
call BDOS_ENTRY_POINT
70+
71+
;; Did that succeed?
72+
cp 00
73+
ret
4174

4275
;;;
4376
;;; The message displayed if no command-line argument was present.
4477
;;;
78+
DELETE_FAILED_MESSAGE:
79+
db "Deleting the file failed.", 0x0a, 0x0d, "$"
80+
NOT_FOUND_MESSAGE:
81+
db "The file does does not exist.", 0x0a, 0x0d, "$"
4582
usage_message:
46-
db "Usage: DELETE FILENAME.EXT"
47-
48-
;; note fall-through here :)
49-
newline:
50-
db 0xa, 0xd, "$"
83+
db "Usage: DELETE FILENAME.EXT", 0xa, 0xd, "$"
5184

5285
END

samples/drive.com

-65 Bytes
Binary file not shown.

samples/drive.z80

Lines changed: 0 additions & 56 deletions
This file was deleted.

samples/find.com

7 Bytes
Binary file not shown.

samples/find.z80

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
;; find.asm
2-
;;
3-
;; Show all files which match the given glob/pattern.
1+
;; find.asm - Show all files which match the given glob/pattern.
42
;;
53

64
FCB1: EQU 0x5C
@@ -94,8 +92,22 @@ show_result:
9492

9593
push af ; preserve return code from find first/next
9694

97-
ld a,(FCB1) ; Output the drive-letter and separator
98-
add a, 65 - 1
95+
;; If the drive is not-zero that means we have an explicit drive
96+
ld a,(FCB1)
97+
cp 0x00
98+
jr nz, letter_drive
99+
100+
;; So the drive is 0x00, which means we're using the current,
101+
;; or default, drive. Find it.
102+
ld c,25
103+
call 0x0005
104+
105+
;; Add one, now we can fall-through to the ASCII conversiion
106+
inc a
107+
108+
letter_drive:
109+
; 1 means A, 2 for B, etc
110+
add a,'A' -1
99111
call print_character
100112

101113
ld a, ':'
@@ -186,16 +198,9 @@ call_bdos_and_return:
186198
;;; The message displayed if no command-line argument was present.
187199
;;;
188200
usage_message:
189-
db "Usage: LOCATE pattern"
190-
201+
db "Usage: FIND pattern"
191202
;; note fall-through here :)
192203
newline:
193204
db 0xa, 0xd, "$"
194205

195-
;;; ***
196-
;;; If this is non-zero we skip showing the user-number
197-
;;;
198-
SHOW_USER_MARKER:
199-
db 0
200-
201206
END

samples/intest.com

-2 Bytes
Binary file not shown.

samples/intest.z80

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,6 @@ C_RAWIO_PROMPT:
259259
DB " Echo should NOT be enabled.", 0x0a, 0x0d
260260
DB " Press 'q' to proceed/complete this test.", 0x0a, 0x0d, "$"
261261

262-
C_RAWIO_BUFFER:
263-
DB 0x00, "$"
264-
265262
C_RAWIO_SPINNER_1:
266263
DB "x", 0x08, "$"
267264
C_RAWIO_SPINNER_2:

samples/read.com

-1 Bytes
Binary file not shown.

samples/read.z80

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
FCB1: EQU 0x5C
1717
DMA: EQU 0x80
18-
DMA_LEN: EQU 128
1918
BDOS_ENTRY_POINT: EQU 5
2019
BDOS_OUTPUT_STRING: EQU 9
2120
BDOS_READ_FILE: EQU 20
@@ -189,16 +188,11 @@ show_a_register:
189188
ret
190189

191190
usage_message:
192-
db "Usage: READ FILENAME.EXT"
193-
;; note fall-through here :)
194-
newline:
195-
db 0xa, 0xd, "$"
191+
db "Usage: READ FILENAME.EXT", 0xa, 0xd, "$"
196192
FAILURE:
197193
db "Unexpected value reading file at record $"
198194

199195
OPEN_FAILED:
200196
db "opening the file failed.", 0x0a, 0x0d, "$"
201197

202-
RECORD:
203-
DB 0x00
204198
END

samples/ret.com

-10 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)