Skip to content

Commit

Permalink
Add DOS support
Browse files Browse the repository at this point in the history
  • Loading branch information
io12 committed May 29, 2020
1 parent 0b96025 commit c67edd3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bootmine
*.img
*.com
bx_enh_dbg.ini
lst
TAGS
23 changes: 17 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
QEMU = qemu-system-i386
BOCHS = bochs
NASM = nasm
DOSBOX = dosbox
DOSEMU = dosemu

.PHONY: all clean qemu bochs
.PHONY: all clean qemu bochs dosbox dosemu

all: bootmine
all: bootmine.img bootmine.com

bootmine: mine.asm
bootmine.img: mine.asm
$(NASM) $< -o $@

bootmine.com: mine.asm
$(NASM) -DDOS $< -o $@

clean:
rm -f bootmine
rm -f bootmine.img bootmine.com

qemu: bootmine
qemu: bootmine.img
$(QEMU) $<

bochs: bootmine
bochs: bootmine.img
$(BOCHS) -q -f bochsrc.txt

dosbox: bootmine.com
$(DOSBOX) $<

dosemu: bootmine.com
$(DOSEMU) $<
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ make

## Installing

This 512-byte file `bootmine`, can be written to the first sector of a floppy disk (or USB drive), with a command like `dd if=bootmine of=/dev/sdb`. Keep in mind that this will effectively destroy all data on the drive.
This 512-byte file `bootmine.img`, can be written to the first sector of a floppy disk (or USB drive), with a command like `dd if=bootmine.img of=/dev/sdb`. Keep in mind that this will effectively destroy all data on the drive.

### Emulation

Expand Down
30 changes: 23 additions & 7 deletions mine.asm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ cpu 686
;; Words in 16 bit x86 are 2 bytes
%assign WordSize 2

;; Load address of DOS COM executables (relative to the cs segment)
%assign DosComLoadAddress 0x100

;; This is the value to store in segment register to access the VGA text buffer.
;; In 16 bit x86, segmented memory accesses are of the form:
;;
Expand Down Expand Up @@ -68,7 +71,13 @@ cpu 686
;; halves the amount of bombs.
%assign BombFreq 0b111

;; BootMine is supported as both a DOS game and a boot sector game :)
;; To build for DOS, run NASM with -DDOS and name the file with a .com extension
%ifdef DOS
org DosComLoadAddress
%else
org BootSector.Begin
%endif

;; Entry point: set up graphics and run game
BootMine:
Expand Down Expand Up @@ -167,8 +176,9 @@ PopulateTextBuf:
; Save di since it will be clobbered later
push di
; Load adjacent cell offset from Dirs array into ax. Since the offset register
; is bp, the segment register used is ss, which zero. This makes the memory
; access relative to the start of RAM instead of the start of the text buffer.
; is bp, the segment register used is ss, which is the same as cs. This makes
; the memory access relative to BootMine's code instead of the start of the
; text buffer.
movsx ax, byte [bp + Dirs - 1]
; Set di = pointer to adjacent cell
add di, ax
Expand Down Expand Up @@ -527,11 +537,17 @@ GameOver:
;; * bp - pointer to string
;; * bx - color of string
GameEndHelper:
; Reset es segment register so printing the game end message works correctly.
; When running as a boot sector, cs=0, so this will reset es to 0. When
; running as a DOS COM executable, the original value of es will be the same
; as cs. (COM executables set each segment register to the same value)
;
; es = cs
mov di, cs
mov es, di

mov ax, 0x1300
mov dx, ((TextBuf.Height / 2) << 8) | (TextBuf.Width / 2 - GameOverStr.Len / 2)
; es = 0
xor di, di
mov es, di
int 0x10

;; Wait for restart key to be pressed, then restart game
Expand All @@ -548,12 +564,12 @@ WaitRestart:
%warning Code is CodeSize bytes

CodeEnd:
%ifndef DOS
; Pad to size of boot sector, minus the size of a word for the boot sector
; magic value. If the code is too big to fit in a boot sector, the `times`
; directive uses a negative value, causing a build error.
times (BootSector.Size - WordSize) - CodeSize db 0

; Boot sector magic
dw 0xaa55

;; Boot sector ends here
%endif

0 comments on commit c67edd3

Please sign in to comment.