I am writing an os. I am doing parts in C to get started and then converting to ASM. I use
Code:
zcc +sos -crt0=my_crt0.s
to compile. Here is the main part:
Code:
__sfr __at 0x10 lcd_command;
__sfr __at 0x11 lcd_data;

#ifdef __linux__
#include <stdint.h>
uint8_t lcd_command, lcd_data;
#endif


#define LCD_6_BIT 0
#define LCD_8_BIT 1
#define LCD_ENABLE 3
#define LCD_DOWN 5
#define LCD_RIGHT 7
#define LCD_ROW 0x80
#define LCD_COL 0x20
#define LCD_CONTRAST 0xC0


typedef unsigned char u8;


u8 *cursor_x = (u8*)0x8000;
u8 *cursor_y = (u8*)0x8001;


#include "font.h"

void lcdDelay() __naked {
        __asm
                call $+3
                nop
                ret
        __endasm;
}

void lcdSetXY( int x, int y) {
        lcd_command = x + LCD_COL;
        lcdDelay();
        lcd_command = y + LCD_ROW;
        lcdDelay();
}

void lcdInit(void) {
        lcd_command = LCD_8_BIT;
        lcdDelay();
        lcd_command = LCD_ENABLE;
        lcdDelay();
        lcd_command = 0xf0;
        lcdDelay();
        lcd_command = 0x40;
        lcdDelay();
        lcd_command = LCD_DOWN;
        lcdDelay();
}

int putchar(char c) __z88dk_fastcall {
    __asm
        ld a,(_cursor_x)
        add a,0x20
        out (0x10),a
        call _lcdDelay

        ld a,(_cursor_y)
        add a,0x80
        out (0x10),a

        ld a,l
        sub a,0x20
        jr c,_putchar_err
        ld l,a
        ld h,0
        add hl,hl
        ld de,_CHAR_MAP
        add hl,de
        ld de,(hl)
        ld b,8
    _putchar_loop:
        ld a,(de)
        inc de
        out ($11),a
        call _lcdDelay
        djnz _putchar_loop
        ld hl,0
        jr _putchar_exit
    _putchar_err:
        ld hl,1
    _putchar_exit:
        push hl
        ld a,(_cursor_x)
        inc a
        cp 12
        jr c,_putchar_y_noinc
        ld a,(_cursor_y)
        inc a
        ld (_cursor_y),a
        xor a
    _putchar_y_noinc:
        ld (_cursor_x),a
        pop hl
    __endasm;
}
The ifdef Linux is so that my lsp doesn't complain as much.
Anyways here are some main() definitions and their output. https://imgur.com/a/main-c-outputs-4si8NwI
The evidence would suggest your `cursor_x` and `cursor_y` are always remaining zero, especially as supported by your "67" test, which seems to instead write "6" twice at (0,0). Your 256 bytes of scratch ram at 0x8000 (appData) should be fine.
Yes but why wont B be at 0,5 and 6 at 0,6 , this also means that along with cursor_x and y not being updated the LCD coordinate set at the start of the ASM also dosen't work. I have no idea at all of what is causing this. When I had the entire routine in C It also didn't work.
Sorry for the double post
Okay now I am even more confused.
The new code (same except for start of putchar)
Code:

int putchar(char c) __z88dk_fastcall {
    lcdSetXY(*cursor_x, *cursor_y);
    __asm
        ;ld a,(_cursor_x)
        ;add a,0x20
        ;out (0x10),a
        ;call _lcdDelay

        ;ld a,(_cursor_y)
        ;add a,0x80
        ;out (0x10),a

        ld a,l
        sub a,0x20
        jr c,_putchar_err
        ld l,a
        ld h,0
        add hl,hl
        ld de,_CHAR_MAP
        add hl,de
        ld de,(hl)
        ld b,8
    _putchar_loop:
        ld a,(de)
        inc de
        out ($11),a
        call _lcdDelay
        djnz _putchar_loop
        ld hl,0
        jr _putchar_exit
    _putchar_err:
        ld hl,1
    _putchar_exit:
        push hl
        ld a,(_cursor_x)
        inc a
        cp 12
        jr c,_putchar_y_noinc
        ld a,(_cursor_y)
        inc a
        ld (_cursor_y),a
        xor a
    _putchar_y_noinc:
        ld (_cursor_x),a
        pop hl
    __endasm;
}

and

Code:
 int main(void) {
    lcdInit();
    *cursor_x = 6;
    *cursor_y = 0;
    putchar(*cursor_x+'0');
    putchar(*cursor_x+'0');
    return 0;
}
Now from this the output is.
I just realized that lcdSetXY corrupts hl and I use fastcall so the char was passed in hl. Here is the output with the code fix (still need to fix updating cursor_x and cursor_y and fix the ASM version of lcdSetXY).

Sorry for triple post
Out of curiosity in the ASM I replaced (_cursor_x) with (0x8000) and (_cursor_y) with (0x8001) and it worked.
At first I had no idea why but then I realized. In c variables are on the stack and I had declared cursor_x and y as vars instead of a define.
The reason this dosen't work is because _cursor_x and y would assemble to their offsets from the stack pointer (prolly 2 and 4) so I was reading (0x0002) and (0x0004). I had discovered one fix but another better one is to use define instead so that is what I am using know.

TLDR fixed code :

Code:
#define cursor_x (u8*)0x8000
#define cursor_y (u8*)0x8001
#define _cursor_x 0x8000
#define _cursor_y 0x8001

and

Code:
int putchar(char c) __z88dk_fastcall {
    __asm
        ld a,(_cursor_x)
        add a,0x20
        out (0x10),a
        call _lcdDelay

        ld a,(_cursor_y)
        add a,0x80
        out (0x10),a

        ld a,l
        sub a,0x20
        jr c,_putchar_err
        ld l,a
        ld h,0
        add hl,hl
        ld de,_CHAR_MAP
        add hl,de
        ld de,(hl)
        ld b,8
    _putchar_loop:
        ld a,(de)
        inc de
        out ($11),a
        call _lcdDelay
        djnz _putchar_loop
        ld hl,0
        jr _putchar_exit
    _putchar_err:
        ld hl,1
    _putchar_exit:
        ld a,(_cursor_x)
        inc a
        cp 12
        jr c,_putchar_y_noinc
        ld a,(_cursor_y)
        inc a
        ld (_cursor_y),a
        xor a
    _putchar_y_noinc:
        ld (_cursor_x),a
    __endasm;
}
And this all works. 🙂
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement