Sherlock: annotated disassembly

Routines

Prev: C37D Up: Map Next: C3E4
C388: PLOT-CHAR: draw one character glyph at HL, shifted by C
Computes the source glyph address as A*8 + $3C00 - which is exactly the real 128K/48K ROM's own character-bitmap layout (space, code 32, sits at $3D00 = $3C00 + 32*8), confirming A is a plain ASCII code here, not some internal token.
For each of the glyph's 8 pixel rows, shifts the byte right by C bits (carrying into the next screen byte, via the 'or c' merge at C3B0/C3CD) so that text can be printed at any pixel column, not just 8-pixel-aligned ones - this is why the routine is more involved than a plain 1:1 glyph copy.
Input
A The character
HL The screen address
C The pixel shift
Output
The glyph drawn; all registers kept
C388 PUSH AF Save everything - A is the character to plot
C389 PUSH BC Save BC on the stack
C38A PUSH DE Save DE on the stack
C38B PUSH HL Save HL on the stack
C38C LD L,A HL = A (as a word)
C38D LD H,$00 Load H with $00
C38F ADD HL,HL HL = A * 8...
C390 ADD HL,HL Double HL (shift it left one bit)
C391 ADD HL,HL Double HL (shift it left one bit)
C392 LD DE,$3C00 ...plus the font table base ($3C00: the ROM layout)
C395 ADD HL,DE Add DE to HL
C396 EX DE,HL DE = this glyph's address; HL = the screen position
C397 POP HL Restore HL from the stack
C398 PUSH HL Save HL on the stack
C399 LD B,$08 8 pixel rows to plot
C39B LD A,(DE) Fetch this row of the glyph
C39C PUSH BC Save BC on the stack
C39D PUSH AF Save AF on the stack
C39E DEC C Is the column-shift zero (byte-aligned print)?
C39F INC C Add one to C
C3A0 LD B,$FF Load B with $FF
C3A2 JR Z,$C3AB Fast path: no shifting needed
C3A4 SRL A Shift the glyph byte right by the column offset...
C3A6 SRL B ...building a matching mask alongside it
C3A8 DEC C Count C down; loop until zero
C3A9 JR NZ,$C3A4 If C is not zero, go back to C3A4
C3AB LD C,A Copy A into C
C3AC LD A,B Copy B into A
C3AD CPL Clear the bits this glyph will occupy...
C3AE AND (HL) Keep only the bits of A that are set in the byte HL points at
C3AF OR C ...and merge the shifted glyph bits in
C3B0 LD (HL),A Store A at the address HL points to
C3B1 POP AF Restore the row counter
C3B2 POP BC Restore BC from the stack
C3B3 DEC C Fast-aligned path rejoins here (from the sign-flag branch above)
C3B4 INC C Add one to C
C3B5 JR Z,$C3D0 If C has become zero, skip ahead to C3D0
C3B7 PUSH BC Remaining shift = 8 - column offset (the overflow into
C3B8 PUSH AF the next screen byte)
C3B9 LD A,$08 C = 8 - C
C3BB SUB C Subtract C from A
C3BC LD C,A Copy A into C
C3BD POP AF Restore AF from the stack
C3BE LD B,$FF Shift the glyph byte left by that remaining amount...
C3C0 SLA A ...building a matching mask alongside it
C3C2 SLA B Shift B left, C times
C3C4 DEC C Subtract one from C
C3C5 JR NZ,$C3C0 If C is not zero, go back to C3C0
C3C7 LD C,A C = A; A = B complemented
C3C8 LD A,B Copy B into A
C3C9 CPL Flip every bit of A
C3CA INC HL Move to the next screen byte along
C3CB AND (HL) Clear the bits this glyph will occupy there...
C3CC OR C Set in A the bits that are set in C
C3CD LD (HL),A ...and merge the overflow bits in
C3CE DEC HL Move back (net: one screen byte to the right overall)
C3CF POP BC Restore BC from the stack
C3D0 INC DE Step DE on to the next byte
C3D1 INC H Next pixel row down
C3D2 DJNZ $C39B Count B down and, while it is not zero, go back to C39B
C3D4 POP HL Restore caller's registers
C3D5 POP DE Restore DE from the stack
C3D6 POP BC Restore BC from the stack
C3D7 LD A,C C = the new column-shift for the next character:
C3D8 ADD A,$07 add the glyph width (7 wide, since 1 col overlaps)...
C3DA CP $08 ...wrapping back into 0-7 and bumping the screen
C3DC JR C,$C3E1 ...column (inc L) when it does
C3DE SUB $08 Subtract $08 from A
C3E0 INC L Add one to L
C3E1 LD C,A Copy A into C
C3E2 POP AF Restore the flags this routine was called with
C3E3 RET Return to the caller
Prev: C37D Up: Map Next: C3E4