Compact 64 column print routine

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
arkannoyed
Microbot
Posts: 111
Joined: Mon Feb 05, 2018 9:56 am

Re: Compact 64 column print routine

Post by arkannoyed » Fri Mar 16, 2018 3:32 pm

Don't you just hate it when you find another place to save a byte...but it means reorganizing code for about an hour!

Oh, and how fortunate that the change to the routine and swapping a few registers around lets me hide the '3' character in there too, thus saving another byte!

Now fully working and down to 95 bytes! :lol:

I really need to comment the source, which I promise I'll do and tidy it up a bit.
0 x

User avatar
arkannoyed
Microbot
Posts: 111
Joined: Mon Feb 05, 2018 9:56 am

Re: Compact 64 column print routine

Post by arkannoyed » Fri Mar 16, 2018 4:15 pm

And heres the code, actually commented this time! Well, I suppose theres always a first time for everything! :lol:

Code: Select all

;15 bit 64 column format
;
;super compact 5 byte high characters
;
;BC holds screen address, HI-byte -1
;DE is message address
;End of Message byte is any value with BIT 7 set
;message must be an even number of characters long (padded with space if reqd.)
;Call routine at 0FA00h (64000d)
;
;97 bytes if we include the trailing 00h,00h (space CHR)
;95 bytes otherwise
;
;Characters A-Z...0-9..spc.
;
;Arkannoyed March 2018
;
;Special Thanks to Einar!

org 0f9fah

          ld de,example_message
          ld bc,3f00h       ;screen address hi-byte must start -1

start:
          ld a,(de)         ;CHR byte into A
          ld (get_bytes+1),a;write it to direct input (HL) load
          inc de            ;next CHR
          add a,a           ;test bit 7 for end marker
          ret c             ;RET if end of message

          sbc a,h           ;useless instruction!!
          push bc           ;together these 2 instructions create CHR 3 (9Ch,C5h)
                            ;db %10011100,%11000101        ;3 code 56h      ;9ch,0c5h
                            ;3 = code 07h
get_bytes:
          ld hl,(0fa00h)    ;get 2 data bytes
lp0:
          inc b             ;down 1 screen line
          ld a,(bc)         ;SCReen byte into A
          or 10h            ;set the marker for 4 bits

          add a,a           ;add the space between chars
lp1:
          add hl,hl         ;propagate the data into the Carry
          rla               ;and then the result into A
          jr nc,lp1         ;repeat if we've not put the marker into the Carry
          
          ld (bc),a         ;replace the SCReen byte
          bit 2,b           ;test if we've done 5 bytes
          jr z,lp0          ;repeat if not

          pop bc            ;restore SCReen address
          ld a,e            ;message address LO byte into A
          and 01h           ;test if odd or even
          add a,c           ;add the result to A
          ld c,a            ;update the SCReen address

                            ;db %11000110,%01110011        ;E code 20h      ;0c6h,73h
          add a,73h         ;code for CHR E = doesn't do anything to affect the routine

          db 18h            ;jr start vector DCh which is the first byte of CHR D

          db %11011100,%11010110        ;D code 23h      ;0dch,0d6h
          db %11011010,%01010111        ;A code 25h      ;0dah,57h
          db %01011100,%11010111        ;B code 27h      ;5ch,0d7h
          db %01000110,%01110010        ;C code 29h      ;46h,72h
          db %00100100,%01001001        ;I code 2bh *    ;24h,49h
          db %01110011                  ;F code 2ch      ;49h,73h
          db %11010100,%01010110        ;O code 2eh *    ;0d4h,56h
          db %01110010                  ;G code 2fh      ;56h,72h
          db %11011010,%10110111        ;H code 31h      ;0dah,0b7h
          db %00100100,%11101001        ;T code 33h *    ;24h,0e9h
          db %10110110                  ;V code 34h      ;0e9h,0b6h
          db %11011100,%11100100        ;J code 36h      ;0dch,0e4h
          db %01011010,%10110111        ;K code 38h      ;5ah,0b7h
          db %01001110,%10010010        ;L code 3ah      ;4eh,92h
          db %11011010,%10111111        ;M code 3ch      ;0dah,0bfh
          db %01001000,%11010111        ;P code 3eh      ;48h,0d7h
          db %01011010,%11010111        ;R code 40h *    ;5ah,0d7h
          db %01010110                  ;Q code 41h      ;0d7h,56h
          db %00011100,%01110001        ;S code 43h      ;1ch,71h
          db %11010100,%10110110        ;U code 45h      ;0d4h,0b6h
          db %11111010,%10110111        ;W code 47h      ;0fah,0b7h
          db %01011010,%10110101        ;X code 49h      ;5ah,0b5h
          db %00100100,%10110101        ;Y code 4bh      ;24h,0b5h
          db %01001110,%11100101        ;Z code 4dh      ;4eh,0e5h
          db %11011010,%11010110        ;N code 4fh *    ;0dah,0d6h
          db %11010110                  ;0 code 50h      ;0d6h,0d6h
          db %00101110,%01011001        ;1 code 52h      ;2eh,59h
          db %01001110,%11000101        ;2 code 54h      ;4eh,0c5h
          db %00011100,%11110011        ;5 code 56h *    ;1ch,0f3h
          db %10010010                  ;4 code 57h      ;0f3h,92h
          db %10100100,%11100100        ;7 code 59h      ;0a4h,0e4h
          db %10010100,%01010101        ;9 code 5bh *    ;9ch,55h
          db %01010101                  ;8 code 5ch *    ;55h,55h
          db %01010011                  ;6 code 5dh *    ;55h,53h
          db %00000000,%00000000        ;spc cd 5fh      ;00h,00h


example_message:
          db 25h,27h,29h,23h,20h,2ch,2fh,31h,2bh,36h,38h,3ah,3ch,4fh,2eh,3eh,41h,40h,43h,33h,45h,34h,47h,49h,4bh,4dh,50h,52h,54h,07h,57h,56h,5dh,59h,5ch,5bh,5fh,0ffh
0 x

User avatar
arkannoyed
Microbot
Posts: 111
Joined: Mon Feb 05, 2018 9:56 am

Re: Compact 64 column print routine

Post by arkannoyed » Fri Mar 16, 2018 4:49 pm

..And probably the final one today, saving another byte by changing the shape of the '9' to share it with 'L'

94 bytes!

Code: Select all

db %11011100,%11010110        ;D code 23h      ;0dch,0d6h
          db %11011010,%01010111        ;A code 25h      ;0dah,57h
          db %01011100,%11010111        ;B code 27h      ;5ch,0d7h
          db %01000110,%01110010        ;C code 29h      ;46h,72h
          db %00100100,%01001001        ;I code 2bh *    ;24h,49h
          db %01110011                  ;F code 2ch      ;49h,73h
          db %11010100,%01010110        ;O code 2eh *    ;0d4h,56h
          db %01110010                  ;G code 2fh      ;56h,72h
          db %11011010,%10110111        ;H code 31h      ;0dah,0b7h
          db %00100100,%11101001        ;T code 33h *    ;24h,0e9h
          db %10110110                  ;V code 34h      ;0e9h,0b6h
          db %11011100,%11100100        ;J code 36h      ;0dch,0e4h
          db %01011010,%10110111        ;K code 38h      ;5ah,0b7h
          db %01001110,%10010010        ;L code 3ah      ;4eh,92h
          db %01010101                  ;9 code 3bh *    ;92h,55h
          db %01010101                  ;8 code 3ch *    ;55h,55h
          db %01010011                  ;6 code 3dh *    ;55h,53h
          db %11011010,%10111111        ;M code 3fh      ;0dah,0bfh
          db %01001000,%11010111        ;P code 41h      ;48h,0d7h
          db %01011010,%11010111        ;R code 43h *    ;5ah,0d7h
          db %01010110                  ;Q code 44h      ;0d7h,56h
          db %00011100,%01110001        ;S code 46h      ;1ch,71h
          db %11010100,%10110110        ;U code 48h      ;0d4h,0b6h
          db %11111010,%10110111        ;W code 4ah      ;0fah,0b7h
          db %01011010,%10110101        ;X code 4ch      ;5ah,0b5h
          db %00100100,%10110101        ;Y code 4eh      ;24h,0b5h
          db %01001110,%11100101        ;Z code 50h      ;4eh,0e5h
          db %11011010,%11010110        ;N code 52h *    ;0dah,0d6h
          db %11010110                  ;0 code 53h      ;0d6h,0d6h
          db %00101110,%01011001        ;1 code 55h      ;2eh,59h
          db %01001110,%11000101        ;2 code 57h      ;4eh,0c5h
          db %00011100,%11110011        ;5 code 59h *    ;1ch,0f3h
          db %10010010                  ;4 code 5ah      ;0f3h,92h
          db %10100100,%11100100        ;7 code 5ch      ;0a4h,0e4h
          db %00000000,%00000000        ;spc cd 5eh      ;00h,00h


example_message:
          db 25h,27h,29h,23h,20h,2ch,2fh,31h,2bh,36h,38h,3ah,3fh,52h,2eh,41h,44h,43h,46h,33h,48h,34h,4ah,4ch,4eh,50h,53h,55h,57h,07h,5ah,59h,3dh,5ch,3ch,3bh,5eh,0ffh
0 x

User avatar
arkannoyed
Microbot
Posts: 111
Joined: Mon Feb 05, 2018 9:56 am

Re: Compact 64 column print routine

Post by arkannoyed » Fri Mar 16, 2018 5:00 pm

Oh well! Never say never!

by sharing A and B, a slight modification to B, but it still looks like B!

93 bytes. :shock:

Code: Select all

db %11011100,%11010110        ;D code 23h      ;0dch,0d6h
          db %11011010,%01010111        ;A code 25h      ;0dah,57h
          db %11010111                  ;B code 26h      ;57h,0d7h
          db %01000110,%01110010        ;C code 28h      ;46h,72h
          db %00100100,%01001001        ;I code 2ah *    ;24h,49h
          db %01110011                  ;F code 2bh      ;49h,73h
          db %11010100,%01010110        ;O code 2dh *    ;0d4h,56h
          db %01110010                  ;G code 2eh      ;56h,72h
          db %11011010,%10110111        ;H code 30h      ;0dah,0b7h
          db %00100100,%11101001        ;T code 32h *    ;24h,0e9h
          db %10110110                  ;V code 33h      ;0e9h,0b6h
          db %11011100,%11100100        ;J code 35h      ;0dch,0e4h
          db %01011010,%10110111        ;K code 37h      ;5ah,0b7h
          db %01001110,%10010010        ;L code 39h      ;4eh,92h
          db %01010101                  ;9 code 3ah *    ;92h,55h
          db %01010101                  ;8 code 3bh *    ;55h,55h
          db %01010011                  ;6 code 3ch *    ;55h,53h
          db %11011010,%10111111        ;M code 3eh      ;0dah,0bfh
          db %01001000,%11010111        ;P code 40h      ;48h,0d7h
          db %01011010,%11010111        ;R code 42h *    ;5ah,0d7h
          db %01010110                  ;Q code 43h      ;0d7h,56h
          db %00011100,%01110001        ;S code 45h      ;1ch,71h
          db %11010100,%10110110        ;U code 47h      ;0d4h,0b6h
          db %11111010,%10110111        ;W code 49h      ;0fah,0b7h
          db %01011010,%10110101        ;X code 4bh      ;5ah,0b5h
          db %00100100,%10110101        ;Y code 4dh      ;24h,0b5h
          db %01001110,%11100101        ;Z code 4fh      ;4eh,0e5h
          db %11011010,%11010110        ;N code 51h *    ;0dah,0d6h
          db %11010110                  ;0 code 52h      ;0d6h,0d6h
          db %00101110,%01011001        ;1 code 54h      ;2eh,59h
          db %01001110,%11000101        ;2 code 56h      ;4eh,0c5h
          db %00011100,%11110011        ;5 code 58h *    ;1ch,0f3h
          db %10010010                  ;4 code 59h      ;0f3h,92h
          db %10100100,%11100100        ;7 code 5bh      ;0a4h,0e4h
          db %00000000,%00000000        ;spc cd 5dh      ;00h,00h


example_message:
          db 25h,26h,28h,23h,20h,2bh,2eh,30h,2ah,35h,37h,39h,3eh,51h,2dh,40h,43h,42h,45h,32h,47h,33h,49h,4bh,4dh,4fh,52h,54h,56h,07h,59h,58h,3ch,5bh,3bh,3ah,5dh,0ffh
0 x

User avatar
arkannoyed
Microbot
Posts: 111
Joined: Mon Feb 05, 2018 9:56 am

Re: Compact 64 column print routine

Post by arkannoyed » Sun Mar 18, 2018 10:11 am

Now 92 bytes. Getting closer to Djnz48’s initial attempt of 89. Only 3 to go, though Im out of ideas as to where to save any more at the moment.
0 x

User avatar
RMartins
Manic Miner
Posts: 331
Joined: Thu Nov 16, 2017 3:26 pm

Re: Compact 64 column print routine

Post by RMartins » Sun Mar 18, 2018 1:41 pm

You can probably shave off 2 more bytes by changing your ORG address, so that 'get_bytes+1' matches the definition of a letter. Maybe W?

You can also reduce the overal footprint by using the 7th bit of the last char in message instead of adding an entire byte to end the message.
This will improve the footprint for each added message.

Alternatively you can concat messages and mark the first character 7th bit as terminator of previous message, if the test is not done on first char or is only done after processing increment.
NOTE :This obviously precludes single char messages.
1 x

User avatar
arkannoyed
Microbot
Posts: 111
Joined: Mon Feb 05, 2018 9:56 am

Re: Compact 64 column print routine

Post by arkannoyed » Sun Mar 18, 2018 5:21 pm

Altering the way the message terminates is possible but not without making the routine larger. Nice idea regarding the address being substituted for a character data set, however it does seriously restrict the number of locations that the routine can be located at. Currently it can go pretty much anywhere. Even hidden in 3 attr blanked lines of the screen maybe at the bottom! We could cheat a little and use ‘S’ and ‘5’ A’s the same character, also ‘0’ and ‘O’, but I don’t like doing that. That would though take it under 90 bytes! I think 92 might just be about the best I can do. Maybe time to add in the lower case characters and see how it grows in size.
1 x

User avatar
djnzx48
Microbot
Posts: 163
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Compact 64 column print routine

Post by djnzx48 » Mon Mar 19, 2018 9:02 am

Have you considered using delta encoding to reduce the size of the font? In this modified font, each row only has up to four possible rows that can follow it. That means that you can store the first row using 3 bits, then use 2 bits each for the remaining four rows, giving 11 bits overall per character. It's still not a particularly nice size to work with, but it might be possible to find more characters that can be merged together with this method.

Image
2 x

User avatar
arkannoyed
Microbot
Posts: 111
Joined: Mon Feb 05, 2018 9:56 am

Re: Compact 64 column print routine

Post by arkannoyed » Mon Mar 19, 2018 4:58 pm

Ahhh, yes, I like that. I have been considering some sort of compression, and I'd struggled to manage an overall improvement over the whole font. Although this makes a few changes that are less than ideal, its certainly a possibility. I've adapted a data structure from my 3D Chess engine, where the routine reads just 1 bit per byte. It relies upon being able to address only up to 32 bytes with interleaved data.

I've managed to encode 31 of 37 characters to fit within the allowed size so far. The routine to display them is currently 50 bytes.

Do, its incomplete, but a total of 50 bytes prog and 40 bytes data, making 90. The '5' and '0' could use 'S' and 'O' instead I suppose, and maybe even 'I' could be '1', which would save a bit more, but still fall just short.

I could encode this CHR set with its in line compression and see what I get.
1 x

User avatar
RMartins
Manic Miner
Posts: 331
Joined: Thu Nov 16, 2017 3:26 pm

Re: Compact 64 column print routine

Post by RMartins » Mon Mar 19, 2018 7:52 pm

arkannoyed wrote:
Sun Mar 18, 2018 5:21 pm
Altering the way the message terminates is possible but not without making the routine larger. ...
Eventually, but it shouldn't be very different, since we just need to test after processing the char.
Probably just need to move a few instructions, and not much else (assuming we do not lose the contents of the string char).
If it's the test flag we need, a simple pair of EX.AF,AF' might do the trick.
The savings will come one byte for each extra message.

Making the code really dense, does not help you much if you have a lot more data than code. It's a balance, between the too.
1 x

Post Reply