From c4658591bd0b9767eb60033beeb44568e74a8666 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 9 Nov 2019 22:55:15 -0500 Subject: [PATCH] ti/lcd: allow for fonts smaller than 5 pixels That's a lot of code for such a small change, but there's a big difference between 5 pixels and 4 pixels: 4 pixels requires compositing. --- kernel/fnt/5x7.bin | Bin 665 -> 665 bytes kernel/ti/lcd.asm | 171 +++++++++++++++++++++++++++++++++++++++----------- tools/font_compile.pl | 11 +++- 3 files changed, 141 insertions(+), 41 deletions(-) diff --git a/kernel/fnt/5x7.bin b/kernel/fnt/5x7.bin index 178e5f64307d473af48167d66e7b5c80e4ebec41..9589cd3746e8d302b94c94124ad82b4431d1da71 100644 GIT binary patch literal 665 zcmXw1D`LYi3>6eK9P|+a3k?f>1j9kWKtVynz(B(tp|D37G#o4x+>_J3q?RQ;eN&=^ z3{cMZ`=wULLwQ%>S0+oj7`5KM`yWVGKvC6_b7JO{X^6`pI30fIZ}{iZ8o^wY#E$Va z*=hoIF$5Qf*f@;cVRd~mG*Nw-iAVmTf-BzmF-vq2jf5z~Thq2Nk>jDb!mv_PM!iE< zoSPXE#w&*AO>hmHqmZg!0&#k*S{4D41M-C_*`%KjeViS^fZOkgczZs_aYT1O(a)kW;Npo0r*V^C4%X`UCfRZ66%3T97ard!q{MgRtFj)ntchuDWOfhvM^Tu v^FW8FfC1qvvlPUE7)+cMsUb#%hyj`6rb-@)D~ literal 665 zcmXw%iL%=u3`GTH9Kuj6Vbe*bZutMd>=DkB7olDE+9FF5A}Kwew^=5aq+MJ1B(a#X zN$vG_|3}hYkX1FO6hjCx$!N0;AvlR+e`Wq0Y8BjBrP(pw*0vIny&1)MqPF19KJiZd z1gxg|F>BNNXKft0{RyG@7QTq9EMco!w;(dP#E&>uYe~#Ibk$8WqoLo;Sl${(_e}<= z&RbEZ$C_nc(43gmeu0ve`uWn=*(pY*{T-{p`#p}sxAS~&PtdJ#>~5W%7(@bPHA^e; zn_reSMlmaimn&p0%ICEPT>{ZD$itTi57K^E1#yaBqlMje(CgJNfoAZDkp6tQHI-QH zdZ`Gm=XPgyD1=0q3=-T4!V_o3hgBS)$kmr}O!TC`-dzHe|9P<^697moUpZ4pqG$3h gv__4Ba)3+m8_4wUzU8bGIAWy67-Hl{DJPiz10Fsb#sB~S diff --git a/kernel/ti/lcd.asm b/kernel/ti/lcd.asm index f3069cf..2d976b0 100644 --- a/kernel/ti/lcd.asm +++ b/kernel/ti/lcd.asm @@ -26,6 +26,25 @@ ; when active row is 0, Z is FNT_HEIGHT+1, when row is 1, Z is (FNT_HEIGHT+1)*2, ; When row is 8, Z is 0. ; +; *** 6/8 bit columns and smaller fonts *** +; +; If your glyphs, including padding, are 6 or 8 pixels wide, you're in luck +; because pushing them to the LCD can be done in a very efficient manner. +; Unfortunately, this makes the LCD unsuitable for a Collapse OS shell: 6 +; pixels per glyph gives us only 16 characters per line, which is hardly +; usable. +; +; This is why we have this buffering system. How it works is that we're always +; in 8-bit mode and we hold the whole area (8 pixels wide by FNT_HEIGHT high) +; in memory. When we want to put a glyph to screen, we first read the contents +; of that area, then add our new glyph, offsetted and masked, to that buffer, +; then push the buffer back to the LCD. If the glyph is split, move to the next +; area and finish the job. +; +; That being said, it's important to define clearly what CURX and CURY variable +; mean. Those variable keep track of the current position *in pixels*, in both +; axes. +; ; *** Requirements *** ; fnt/mgm ; @@ -37,7 +56,9 @@ .equ LCD_CMD_8BIT 0x01 .equ LCD_CMD_DISABLE 0x02 .equ LCD_CMD_ENABLE 0x03 +.equ LCD_CMD_XDEC 0x04 .equ LCD_CMD_XINC 0x05 +.equ LCD_CMD_YDEC 0x06 .equ LCD_CMD_YINC 0x07 .equ LCD_CMD_COL 0x20 .equ LCD_CMD_ZOFFSET 0x40 @@ -45,19 +66,22 @@ .equ LCD_CMD_CONTRAST 0xc0 ; *** Variables *** -; Current row being written on. In terms of pixels, not of glyphs. During a -; linefeed, this increases by FNT_HEIGHT+1. -.equ LCD_CURROW LCD_RAMSTART -; Current column -.equ LCD_CURCOL @+1 -.equ LCD_RAMEND @+1 +; Current Y position on the LCD, that is, where re're going to spit our next +; glyph. +.equ LCD_CURY LCD_RAMSTART +; Current X position +.equ LCD_CURX @+1 +; two pixel buffers that are 8 pixels wide (1b) by FNT_HEIGHT pixels high. +; This is where we compose our resulting pixels blocks when spitting a glyph. +.equ LCD_BUF @+1 +.equ LCD_RAMEND @+FNT_HEIGHT*2 ; *** Code *** lcdInit: ; Initialize variables xor a - ld (LCD_CURROW), a - ld (LCD_CURCOL), a + ld (LCD_CURY), a + ld (LCD_CURX), a ; Clear screen call lcdClrScr @@ -82,12 +106,8 @@ lcdInit: ld a, LCD_CMD_CONTRAST+0x34 call lcdCmd - ; Enable 6-bit mode. - ld a, LCD_CMD_6BIT - call lcdCmd - - ; Enable X-increment mode - ld a, LCD_CMD_XINC + ; Enable 8-bit mode. + ld a, LCD_CMD_8BIT call lcdCmd ret @@ -109,10 +129,15 @@ lcdCmd: jr lcdWait ; Send data A to LCD -lcdData: +lcdDataSet: out (LCD_PORT_DATA), a jr lcdWait +; Get data from LCD into A +lcdDataGet: + in a, (LCD_PORT_DATA) + jr lcdWait + ; Turn LCD off lcdOff: push af @@ -140,52 +165,126 @@ lcdSetRow: pop af ret -; Send the 5x7 glyph that HL points to to the LCD, at its current position. +; Send the glyph that HL points to to the LCD, at its current position. ; After having called this, the LCD's position will have advanced by one ; position lcdSendGlyph: push af push bc push hl + push ix - ld a, (LCD_CURROW) + ld a, (LCD_CURY) call lcdSetRow - ld a, (LCD_CURCOL) + ld a, (LCD_CURX) + srl a \ srl a \ srl a ; div by 8 call lcdSetCol + ; First operation: read the LCD memory for the "left" side of the + ; buffer. We assume the right side to always be empty, so we don't + ; read it. After having read each line, compose it with glyph line at + ; HL + + ; Before we start, what is our bit offset? + ld a, (LCD_CURX) + and 0b111 + ; that's our offset, store it in C + ld c, a + + ld a, LCD_CMD_XINC + call lcdCmd + ld ix, LCD_BUF ld b, FNT_HEIGHT -.loop: + ; A dummy read is needed after a movement. + call lcdDataGet +.loop1: + ; let's go get that glyph data ld a, (hl) + ld (ix), a + call .shiftIX + ; now let's go get existing pixel on LCD + call lcdDataGet + ; and now let's do some compositing! + or (ix) + ld (ix), a inc hl - call lcdData - djnz .loop + inc ix + djnz .loop1 + + ; Buffer set! now let's send it. + ld a, (LCD_CURY) + call lcdSetRow + + ld hl, LCD_BUF + ld b, FNT_HEIGHT +.loop2: + ld a, (hl) + call lcdDataSet + inc hl + djnz .loop2 + + ; And finally, let's send the "right side" of the buffer + ld a, (LCD_CURY) + call lcdSetRow + ld a, (LCD_CURX) + srl a \ srl a \ srl a ; div by 8 + inc a + call lcdSetCol + + ld hl, LCD_BUF+FNT_HEIGHT + ld b, FNT_HEIGHT +.loop3: + ld a, (hl) + call lcdDataSet + inc hl + djnz .loop3 ; Increase column and wrap if necessary - ld a, (LCD_CURCOL) - inc a - ld (LCD_CURCOL), a - cp 16 - jr nz, .skip + ld a, (LCD_CURX) + add a, FNT_WIDTH+1 + ld (LCD_CURX), a + cp 96-FNT_WIDTH + jr c, .skip ; A < 96-FNT_WIDTH call lcdLinefeed .skip: + pop ix pop hl pop bc pop af ret +; Shift glyph in (IX) to the right C times, sending carry into (IX+FNT_HEIGHT) +.shiftIX: + dec c \ inc c + ret z ; zero? nothing to do + push bc ; --> lvl 1 + xor a + ld b, a + ld a, (ix) + ; TODO: support SRL (IX) and RR (IX) in zasm +.shiftLoop: + srl a + rr b + dec c + jr nz, .shiftLoop + ld (ix), a + ld a, b + ld (ix+FNT_HEIGHT), a + pop bc ; <-- lvl 1 + ret ; Changes the current line and go back to leftmost column lcdLinefeed: push af - ld a, (LCD_CURROW) + ld a, (LCD_CURY) call .addFntH - ld (LCD_CURROW), a + ld (LCD_CURY), a call lcdClrLn ; Now, lets set Z offset which is CURROW+FNT_HEIGHT+1 call .addFntH add a, LCD_CMD_ZOFFSET call lcdCmd xor a - ld (LCD_CURCOL), a + ld (LCD_CURX), a pop af ret .addFntH: @@ -201,8 +300,6 @@ lcdLinefeed: lcdClrX: push af call lcdSetRow - ld a, LCD_CMD_8BIT - call lcdCmd .outer: push bc ; --> lvl 1 ld b, 11 @@ -211,16 +308,14 @@ lcdClrX: xor a call lcdSetCol .inner: - call lcdData + call lcdDataSet djnz .inner ld a, LCD_CMD_XINC call lcdCmd xor a - call lcdData + call lcdDataSet pop bc ; <-- lvl 1 djnz .outer - ld a, LCD_CMD_6BIT - call lcdCmd pop af ret @@ -251,10 +346,10 @@ lcdPutC: pop hl ret .bs: - ld a, (LCD_CURCOL) + ld a, (LCD_CURX) or a ret z ; going back one line is too complicated. ; not implemented yet - dec a - ld (LCD_CURCOL), a + sub FNT_WIDTH+1 + ld (LCD_CURX), a ret diff --git a/tools/font_compile.pl b/tools/font_compile.pl index 3fa8b64..67b781b 100755 --- a/tools/font_compile.pl +++ b/tools/font_compile.pl @@ -3,11 +3,16 @@ use strict; # This script converts "space-dot" fonts to binary "glyph rows". One byte for # each row. In a 5x7 font, each glyph thus use 7 bytes. +# Resulting bytes are aligned to the **left** of the byte. Therefore, for +# a 5-bit wide char, ". . ." translates to 0b10101000 +# Left-aligned bytes are easier to work with when compositing glyphs. my $fn = @ARGV[0]; unless ($fn =~ /.*(\d)x(\d)\.txt/) { die "$fn isn't a font filename" }; my ($width, $height) = ($1, $2); +if ($width > 8) { die "Can't have a width > 8"; } + print STDERR "Reading a $width x $height font.\n"; my $handle; @@ -21,9 +26,9 @@ while (<$handle>) { unless (/( |\.){${width}}\n/) { die "Invalid line format '$_'"; } my @line = split //, $_; my $num = 0; - for (my $i=$width-1; $i>=0; $i--) { - if (@line[$width-$i-1] eq '.') { - $num += (1 << $i); + for (my $i=0; $i<8; $i++) { + if (@line[$i] eq '.') { + $num += (1 << (7-$i)); } } print pack('C', $num);