Link

Display Direction Faced and Level in Dungeon

Table of contents

  1. Introduction
  2. Persist Current Cursor
  3. Display Direction
  4. Display Dungeon Level
  5. Sub-Routines For Displaying Directions
  6. Other Sub-Routines Used
  7. Zero Page Locations Used
  8. What Calls This

Introduction

When in a dungeon, the direction faced and the level of the dungeon the player is on is displayed.

The vector call is to $0872.

0872  4C E2 11      JMP $11E2

But the actual routine is at $11E2.

Persist Current Cursor

First, we call a routine to persist the current cursor. We’ll restore it at the end.

11E2    20 63 12    JSR $1263

Display Direction

We want to print “DIR: {direction}” at (0x07, 0x17).

First we set the cursor to (0x07, 0x17). This is stored in $CE and $CF.

11E5    A9 17       LDA #$17
11E7    85 CF       STA $CF
11E9    A9 07       LDA #$07
11EB    85 CE       STA $CE

Next, we call a routine that displays the following null-terminated string in the game font at the current cursor.

11ED    20 85 0F    JSR $0F85
11F0    C4 C9 D2 BA A0 00    "DIR: "

The direction is stored in $10:

  • 0 = North
  • 1 = East
  • 2 = South
  • 3 = West

We adopt a common switching technique for low-valued enums like this where we use BEQ to branch on zero, decrementing the value we’re switching on as many times as needed to go through all but the final catch-all option.

11F6    A6 10       LDX $10
11F8    F0 08       BEQ $1202       ; if $10 == 0
11FA    CA          DEX
11FB    F0 0B       BEQ $1208       ; if $10 == 1
11FD    CA          DEX
11FE    F0 0E       BEQ $120E       ; if $10 == 2
1200    D0 12       BNE $1214       ; otherwise

1202    20 2F 12    JSR $122F       ; print "NORTH"
1205    4C 17 12    JMP $1217       ; go to dungeon level display

1208    20 43 12    JSR $1243       ; print " EAST"
120B    4C 17 12    JMP $1217       ; go to dungeon level display

120E    20 39 12    JSR $1239       ; print "SOUTH"
1211    4C 17 12    JMP $1217       ; go to dungeon level display

1214    20 4D 12    JSR $124D       ; print " WEST"

Display Dungeon Level

We want to print “L{level}” at (0x0B, 0x00).

First we set the cursor to (0x0B, 0x00). This is stored in $CE and $CF.

1217    A9 00       LDA #$00
1219    85 CF       STA $CF
121B    A9 0B       LDA #$0B
121D    85 CE       STA $CE

Next we load the character L into the accumulator then call a routine the displays the single character in the accumulator at the cursor.

121F    A9 CC       LDA #$CC        ; 'L'
1221    20 A9 0F    JSR $0FA9

Then we load the level the player is on (stored in $0C) into the accumulator, add one to it (as #$00 = L1) and display it, calling a routine that displays the accumulator value as a single digit at the cursor.

1224    A5 0C       LDA $0C
1226    18          CLC
1227    69 01       ADC #$01
1229    20 8B 10    JSR $108B

Finally we restore the saved cursor and return. This is a jump to tail-optimise. The RTS from this jumped-to routine will act as the return from this routine.

122C    4C 6E 12    JMP $126E

Sub-Routines For Displaying Directions

122F    20 85 0F    JSR $0F85
1232    CE CF D2 D4 C8 00           "NORTH"
1238    60          RTS

1239    20 85 0F    JSR $0F85
123C    D3 CF D5 D4 C8 00           "SOUTH"
1242    60          RTS

1243    20 85 0F    JSR $0F85
1246    A0 C5 C1 D3 D4 00           " EAST"
124C    60          RTS

124D    20 85 0F    JSR $0F85
1250    A0 D7 C5 D3 D4 00           " WEST"
1256    60          RTS

Other Sub-Routines Used

$0F85 — display the following null-terminated string at the current cursor location

$0FA9 — display the single character in the accumulator at the current cursor location

$108B — display the single digit number in the accumulator at the current cursor location.

$1263 — persist the current cursor location

$126E — restore the saved cursor location

Zero Page Locations Used

$0C — dungeon level (#$00 = Level 1)

$10 — direction faced (in dungeon)

$CE — cursor location (column)

$CF — cursor location (row)

What Calls This

Two places in ULT4 but I haven’t fully explored those yet.