retrocoding/src/altair8800/cpm/DixMille.asm

606 lines
7.8 KiB
NASM
Raw Normal View History

2024-04-12 09:16:38 +00:00
; DixMille.z80 - a Z80 assembly implementation of the dice game Dixe Mille.
2024-04-12 09:15:23 +00:00
; Copyright (C) 2023 William R. Moore
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
ORG 100H
; Driver logic
START
; LD SP, STACK
LD DE, CLS
CALL PRINTS
LD DE, HOME
CALL PRINTS
LD DE, WELCOME
CALL PRINTS
LD DE, NEWLINE
CALL PRINTS
ROLLUNHELD
LD DE, STOPROLL
CALL PRINTS
LD DE, NEWLINE
CALL PRINTS
CALL ROLLTHEMBONES
CALL HOLDDICE
CALL OUTPUT
CALL COUNTHELD
CP 6
JP Z, DONE
CALL ROLLUNHELD
DONE
JP 00H
COUNTHELD
LD HL, HELD_DICE
ADD A, (HL)
INC L
ADD A, (HL)
INC L
ADD A, (HL)
INC L
ADD A, (HL)
INC L
ADD A, (HL)
INC L
ADD A, (HL)
LD L, 0
RET
ROLLTHEMBONES
LD HL, DICE
PUSH HL
LD HL, HELD_DICE
LD A, (HL)
POP HL
CP 1
JP Z, ROLL2
CALL ROLL
LD DE, DIE1
CALL PRINTS
LD A, (HL)
CALL PRINTFACE
LD DE, NEWLINE
CALL PRINTS
ROLL2
INC L
PUSH HL
LD HL, HELD_DICE
INC L
LD A, (HL)
POP HL
CP 1
JP Z, ROLL3
CALL ROLL
LD DE, DIE2
CALL PRINTS
LD A, (HL)
CALL PRINTFACE
LD DE, NEWLINE
CALL PRINTS
ROLL3
INC L
PUSH HL
LD HL, HELD_DICE
INC L
INC L
LD A, (HL)
POP HL
CP 1
JP Z, ROLL4
CALL ROLL
LD DE, DIE3
CALL PRINTS
LD A, (HL)
CALL PRINTFACE
LD DE, NEWLINE
CALL PRINTS
ROLL4
INC L
PUSH HL
LD HL, HELD_DICE
INC L
INC L
INC L
LD A, (HL)
POP HL
CP 1
JP Z, ROLL5
CALL ROLL
LD DE, DIE4
CALL PRINTS
LD A, (HL)
CALL PRINTFACE
LD DE, NEWLINE
CALL PRINTS
ROLL5
INC L
PUSH HL
LD HL, HELD_DICE
INC L
INC L
INC L
INC L
LD A, (HL)
POP HL
CP 1
JP Z, ROLL6
CALL ROLL
LD DE, DIE5
CALL PRINTS
LD A, (HL)
CALL PRINTFACE
LD DE, NEWLINE
CALL PRINTS
ROLL6
INC L
PUSH HL
LD HL, HELD_DICE
INC L
INC L
INC L
INC L
INC L
LD A, (HL)
POP HL
CP 1
JP Z, ROLLEND
CALL ROLL
LD DE, DIE6
CALL PRINTS
LD A, (HL)
CALL PRINTFACE
LD DE, NEWLINE
CALL PRINTS
; Return back to original position
ROLLEND
DEC L
DEC L
DEC L
DEC L
DEC L
RET
HOLDDIE
CALL INPUTC
CP 00H
JP Z, HOLDDIE
CP 121
JP Z, HOLDDIE_FINISH
CP 89
JP Z, HOLDDIE_FINISH
CP 110
JP Z, HOLDDIE_END
CP 78
JP Z, HOLDDIE_END
CALL HOLDDIE
HOLDDIE_FINISH
LD (HL), 1
HOLDDIE_END
RET
; Ask player to hold any dice
HOLDDICE
LD HL, HELD_DICE
LD A, (HL)
CP 1
JP Z, HD2
LD DE, DYWTH
CALL PRINTS
LD DE, DIE1
CALL PRINTS
CALL HOLDDIE
LD DE, NEWLINE
CALL PRINTS
HD2
INC L
LD A, (HL)
CP 1
JP Z, HD3
LD DE, DYWTH
CALL PRINTS
LD DE, DIE2
CALL PRINTS
CALL HOLDDIE
LD DE, NEWLINE
CALL PRINTS
HD3
INC L
LD A, (HL)
CP 1
JP Z, HD4
LD DE, DYWTH
CALL PRINTS
LD DE, DIE3
CALL PRINTS
CALL HOLDDIE
LD DE, NEWLINE
CALL PRINTS
HD4
INC L
LD A, (HL)
CP 1
JP Z, HD5
LD DE, DYWTH
CALL PRINTS
LD DE, DIE4
CALL PRINTS
CALL HOLDDIE
LD DE, NEWLINE
CALL PRINTS
HD5
INC L
LD A, (HL)
CP 1
JP Z, HD6
LD DE, DYWTH
CALL PRINTS
LD DE, DIE5
CALL PRINTS
CALL HOLDDIE
LD DE, NEWLINE
CALL PRINTS
HD6
INC L
LD A, (HL)
CP 1
JP Z, HD_DONE
LD DE, DYWTH
CALL PRINTS
LD DE, DIE6
CALL PRINTS
CALL HOLDDIE
LD DE, NEWLINE
CALL PRINTS
HD_DONE
; Return back to original position
LD L, 0
RET
; Roll the different dice
ROLL
PUSH BC
ROLL_START
LD C, 0
ROLL_DIE
INC C
LD A, C
CP 7H
JP Z, ROLL_START
CALL INPUTC
CP 00H
JP NZ, ROLL_END
JP ROLL_DIE
ROLL_END
LD (HL), C
POP BC
RET
; Output the dice results
OUTPUT
LD HL, DICE
LD DE, YOUROLLED
CALL PRINTS
LD DE, NEWLINE
CALL PRINTS
LD DE, DIE1
CALL PRINTS
LD A, (HL)
CALL PRINTFACE
PUSH HL
LD HL, HELD_DICE
LD A, (HL)
CP 1
JP Z, MARK_1
JP END_1
MARK_1
LD DE, MARKED
CALL PRINTS
END_1
POP HL
LD DE, NEWLINE
CALL PRINTS
O2
LD DE, DIE2
CALL PRINTS
INC L
LD A, (HL)
CALL PRINTFACE
PUSH HL
LD HL, HELD_DICE
INC L
LD A, (HL)
CP 1
JP Z, MARK_2
JP END_2
MARK_2
LD DE, MARKED
CALL PRINTS
END_2
DEC L
POP HL
LD DE, NEWLINE
CALL PRINTS
O3
LD DE, DIE3
CALL PRINTS
INC L
LD A, (HL)
CALL PRINTFACE
PUSH HL
LD HL, HELD_DICE
INC L
INC L
LD A, (HL)
CP 1
JP Z, MARK_3
JP END_3
MARK_3
LD DE, MARKED
CALL PRINTS
END_3
DEC L
DEC L
POP HL
LD DE, NEWLINE
CALL PRINTS
O4
LD DE, DIE4
CALL PRINTS
INC L
LD A, (HL)
CALL PRINTFACE
PUSH HL
LD HL, HELD_DICE
INC L
INC L
INC L
LD A, (HL)
CP 1
JP Z, MARK_4
JP END_4
MARK_4
LD DE, MARKED
CALL PRINTS
END_4
DEC L
DEC L
DEC L
POP HL
LD DE, NEWLINE
CALL PRINTS
O5
LD DE, DIE5
CALL PRINTS
INC L
LD A, (HL)
CALL PRINTFACE
PUSH HL
LD HL, HELD_DICE
INC L
INC L
INC L
INC L
LD A, (HL)
CP 1
JP Z, MARK_5
JP END_5
MARK_5
LD DE, MARKED
CALL PRINTS
END_5
DEC L
DEC L
DEC L
DEC L
POP HL
LD DE, NEWLINE
CALL PRINTS
O6
LD DE, DIE6
CALL PRINTS
INC L
LD A, (HL)
CALL PRINTFACE
PUSH HL
LD HL, HELD_DICE
INC L
INC L
INC L
INC L
INC L
LD A, (HL)
CP 1
JP Z, MARK_6
JP END_6
MARK_6
LD DE, MARKED
CALL PRINTS
END_6
DEC L
DEC L
DEC L
DEC L
DEC L
POP HL
LD DE, NEWLINE
CALL PRINTS
DEC L
DEC L
DEC L
DEC L
DEC L
RET
; Read 1 character when one is ready
; A is 00H if none are available
INPUTC
PUSH HL
PUSH BC
PUSH DE
LD C, CONST
CALL BDOS
CP 00H
JP Z, INPUTCEND
; LD C, CONST
; CALL BDOS
; CP FFH
; JR Z, INPUTCEND
; LD C, CONIN
; CALL BDOS
LD C, CONIN
CALL BDOS
POP DE
POP BC
POP HL
RET
INPUTCEND
POP DE
POP BC
POP HL
; LD A, 00H
; POP BC
; POP HL
RET
; Print a string
PRINTS
; preserve BC register.
; preserve HL register.
PUSH HL
PUSH BC
LD C, WRITESTR
CALL BDOS
POP BC
POP HL
RET
PRINTFACE
CP 1
JP NZ, CHECKTWO
LD DE, ONE
CALL PRINTS
JP PRINTFACE_END
CHECKTWO
CP 2
JP NZ, CHECKTHREE
LD DE, TWO
CALL PRINTS
JP PRINTFACE_END
CHECKTHREE
CP 3
JP NZ, CHECKFOUR
LD DE, THREE
CALL PRINTS
JP PRINTFACE_END
CHECKFOUR
CP 4
JP NZ, CHECKFIVE
LD DE, FOUR
CALL PRINTS
JP PRINTFACE_END
CHECKFIVE
CP 5
JP NZ, CHECKSIX
LD DE, FIVE
CALL PRINTS
JP PRINTFACE_END
CHECKSIX
CP 6
JP NZ, CHECKBADVALUE
LD DE, SIX
CALL PRINTS
JP PRINTFACE_END
CHECKBADVALUE
LD DE, BADVALUE
CALL PRINTS
PRINTFACE_END
RET
EOS DB "$"
WELCOME DB "WELCOME TO DIX MILLE FOR CP/M!$"
STOPROLL DB "PRESS ANY KEY AT ANY TIME TO STOP ROLLING A DIE!$"
DYWTH DB "DO YOU WANT TO HOLD (Y/N) $"
OPTIMAL DB "NOW, DETERMINING OPTIMAL SCORE: $"
YOUROLLED DB "RESULTS:$"
NEWLINE DB CR, LF, "$"
DIE1 DB "DIE 1: $"
DIE2 DB "DIE 2: $"
DIE3 DB "DIE 3: $"
DIE4 DB "DIE 4: $"
DIE5 DB "DIE 5: $"
DIE6 DB "DIE 6: $"
ONE DB "ONE$"
TWO DB "TWO$"
THREE DB "THREE$"
FOUR DB "FOUR$"
FIVE DB "FIVE$"
SIX DB "SIX$"
MARKED DB " *$"
CLS DB 1bh, '[2J$' ; ANSI clear screen: ESC [ 2 J
HOME DB 1bh, '[H$' ; ANSI go to screen home: ESC [ H
BADVALUE DB "BAD VALUE$"
TOTALHELD DB 0
DICE DB 0, 0, 0, 0, 0, 0
HELD_DICE DB 0, 0, 0, 0, 0, 0
CR equ 0DH
LF equ 0AH
STACK equ 2000H
CONST EQU 0BH
CONIN EQU 01H
BDOS EQU 05H
PRTCHR EQU 02H
WRITESTR EQU 09H
END