In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Discussion about other targets
Post Reply
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Hi,

My code works fine in ROM. All my code does is display some text and then accept text from the keyboard and echos it via teraterm.
My memory map is as follows:

0000H-7FFFH ROM
8000H-FFFFH RAM

Teraterm Output:

**** WELCOME TO 8085 BASIC V1 ****
32K ROM, 32K RAM SYSTEM, 32768 BASIC BYTES FREE
READY.
abc123
abc123

I tried to use some entry points in ROM for char in and out routines in a program I placed in RAM but it doesn't work, it just reboots my 8085 computer? I jump to the ram code from the rom code:

Fragment of ROM CODE:

Code: Select all

.
.
.
LXI H, WELCOME_MSG
CALL PRINT_STRING
BASIC_CMD_LOOP: 
   LXI H, READY_MSG
   CALL PRINT_STRING
   CALL READ_LINE
   LXI H,9000H
   CALL PRINT_STRING
JMP 8000H
.
.
.

Teraterm Output:

**** WELCOME TO 8085 BASIC V1 ****
32K ROM, 32K RAM SYSTEM, 32768 BASIC BYTES FREE
READY.

Details:

World2.c

Code: Select all

#include <stdio.h>

int main()
{
    printf("Hello from z88dk!\n");

    while ( 1 ) {
        int c = getchar();
        printf("<%c>=%d ", c,c);
    }
}
Mon2.c

Code: Select all

#include <stdio.h>

int fputc_cons_native(char c) __naked
{
__asm
    pop     b  ;return address
    pop     h  ;character to print in l
    push    h
    push    b
    MOV      a,l
    call    $00D0
    ret
__endasm;
} 


int fgetc_cons() __naked
{
__asm
    call    $00FB
    mov     l,a     ;Return the result in hl
    mvi      h,0
    ret
__endasm;
}
Compile line:

Code: Select all

zcc +z80 -clib=8085 world2.c mon2.c -pragma-define:CRT_ORG_CODE=0x8000 -pragma-define:CRT_ORG_BSS=0x8000 -pragma-define:REGISTER_SP=0xFFFE -create-app -m8085
The generated code seems to make no sense to me. It doesn't execute my monitor code (shown partially, msg char limits):

Code: Select all

z88dk-dis -o 0x8000 -x a.map a.rom
                    ld        sp,$fffe                      ;[8000] 31 fe ff
                    ld        hl,$ffc0                      ;[8003] 21 c0 ff
                    add       hl,sp                         ;[8006] 39
                    ld        sp,hl                         ;[8007] f9
                    call      $8025                         ;[8008] cd 25 80
                    ld        hl,$0000                      ;[800b] 21 00 00
                    add       hl,sp                         ;[800e] 39
                    ld        ($8067),hl                    ;[800f] 22 67 80
                    call      $8127                         ;[8012] cd 27 81
                    call      $8057                         ;[8015] cd 57 80
                    halt                                    ;[8018] 76
                    jp        $8018                         ;[8019] c3 18 80
                    jp        (hl)                          ;[801c] e9
                    ld        h,h                           ;[801d] 64
                    daa                                     ;[801e] 27
                    add       d                             ;[801f] 82
                    ld        h,e                           ;[8020] 63
                    dec       de                            ;[8021] 1b
                    add       d                             ;[8022] 82
                    nop                                     ;[8023] 00
                    nop                                     ;[8024] 00
                    xor       a                             ;[8025] af
                    ld        hl,$8000                      ;[8026] 21 00 80
                    ld        bc,$006c                      ;[8029] 01 6c 00
                    inc       b                             ;[802c] 04
                    inc       c                             ;[802d] 0c
                    ld        (hl),a                        ;[802e] 77
                    inc       hl                            ;[802f] 23
                    dec       c                             ;[8030] 0d
                    jp        nz,$802e                      ;[8031] c2 2e 80
                    dec       b                             ;[8034] 05
                    jp        nz,$802e                      ;[8035] c2 2e 80
                    ld        ($8069),a                     ;[8038] 32 69 80
                    ld        hl,$8003                      ;[803b] 21 03 80
                    ld        (hl),$13                      ;[803e] 36 13
                    ld        hl,$800d                      ;[8040] 21 0d 80
                    ld        (hl),$15                      ;[8043] 36 15
                    ld        hl,$8017                      ;[8045] 21 17 80
                    ld        (hl),$15                      ;[8048] 36 15
                    ld        hl,$85f0                      ;[804a] 21 f0 85
                    ld        de,$806c                      ;[804d] 11 6c 80
                    ld        bc,$0001                      ;[8050] 01 01 00
                    call      $85b9                         ;[8053] cd b9 85
                    ret                                     ;[8056] c9

                    ret                                     ;[8057] c9

                    ld        a,l                           ;[8058] 7d
                    cpl                                     ;[8059] 2f
                    ld        l,a                           ;[805a] 6f
                    ld        a,h                           ;[805b] 7c
                    cpl                                     ;[805c] 2f
                    ld        h,a                           ;[805d] 67
                    ld        a,e                           ;[805e] 7b
                    cpl                                     ;[805f] 2f
                    ld        e,a                           ;[8060] 5f
                    ld        a,d                           ;[8061] 7a
                    cpl                                     ;[8062] 2f
                    ld        d,a                           ;[8063] 57
                    inc       l                             ;[8064] 2c
                    ret       nz                            ;[8065] c0
                    inc       h                             ;[8066] 24
                    ret       nz                            ;[8067] c0
                    inc       de                            ;[8068] 13
                    ret                                     ;[8069] c9

                    ld        a,d                           ;[806a] 7a
                    or        e                             ;[806b] b3
                    or        h                             ;[806c] b4
                    or        l                             ;[806d] b5
                    jp        z,$8095                       ;[806e] ca 95 80
                    push      de                            ;[8071] d5
                    push      hl                            ;[8072] e5
                    ld        bc,$0000                      ;[8073] 01 00 00
                    push      bc                            ;[8076] c5
                    push      bc                            ;[8077] c5
                    push      bc                            ;[8078] c5
                    call      $809f                         ;[8079] cd 9f 80
                    jr        c,$808c                       ;[807c] 38 0e
                    neg                                     ;[807e] ed 44
                    ld        c,l                           ;[8080] 4d
                    jr        c,$808d                       ;[8081] 38 0a
                    nop                                     ;[8083] ed 38
                    ld        c,$d9                         ;[8085] 0e d9
                    jr        c,$808b                       ;[8087] 38 02
                    ld        a,(de)                        ;[8089] 1a
                    jr        c,$8098                       ;[808a] 38 0c
                    nop                                     ;[808c] ed 38
                    ld        c,$eb                         ;[808e] 0e eb
                    ld        sp,hl                         ;[8090] f9
                    ex        de,hl                         ;[8091] eb
                    ld        d,b                           ;[8092] 50
                    ld        e,c                           ;[8093] 59
                    ret                                     ;[8094] c9

                    pop       bc                            ;[8095] c1
                    pop       hl                            ;[8096] e1
                    pop       de                            ;[8097] d1
                    push      bc                            ;[8098] c5
                    ld        de,$ffff                      ;[8099] 11 ff ff
                    ld        h,d                           ;[809c] 62
                    ld        l,e                           ;[809d] 6b
                    ret                                     ;[809e] c9

                    ld        b,$20                         ;[809f] 06 20
                    jr        c,$80b1                       ;[80a1] 38 0e
                    ld        a,(de)                        ;[80a3] 1a
                    rla                                     ;[80a4] 17
                    ld        (de),a                        ;[80a5] 12
                    inc       de                            ;[80a6] 13
                    ld        a,(de)                        ;[80a7] 1a
                    rla                                     ;[80a8] 17
                    ld        (de),a                        ;[80a9] 12
                    inc       de                            ;[80aa] 13
                    ld        a,(de)                        ;[80ab] 1a
                    rla                                     ;[80ac] 17
                    ld        (de),a                        ;[80ad] 12
                    inc       de                            ;[80ae] 13
                    ld        a,(de)                        ;[80af] 1a
                    rla                                     ;[80b0] 17
                    ld        (de),a                        ;[80b1] 12
                    jr        c,$80b8                       ;[80b2] 38 04
                    ld        a,(de)                        ;[80b4] 1a
                    rla                                     ;[80b5] 17
                    ld        (de),a                        ;[80b6] 12
                    inc       de                            ;[80b7] 13
                    ld        a,(de)                        ;[80b8] 1a
                    rla                                     ;[80b9] 17
                    ld        (de),a                        ;[80ba] 12
                    inc       de                            ;[80bb] 13
                    ld        a,(de)                        ;[80bc] 1a
                    rla                                     ;[80bd] 17
                    ld        (de),a                        ;[80be] 12
                    inc       de                            ;[80bf] 13
                    ld        a,(de)                        ;[80c0] 1a
                    rla                                     ;[80c1] 17
                    ld        (de),a                        ;[80c2] 12
                    jr        c,$80cd                       ;[80c3] 38 08
                    ex        de,hl                         ;[80c5] eb
                    jr        c,$80cc                       ;[80c6] 38 04
                    ld        a,(de)                        ;[80c8] 1a
                    sub       (hl)                          ;[80c9] 96
                    inc       de                            ;[80ca] 13
                    inc       hl                            ;[80cb] 23
                    ld        a,(de)                        ;[80cc] 1a
                    sbc       (hl)                          ;[80cd] 9e
                    inc       de                            ;[80ce] 13
                    inc       hl                            ;[80cf] 23
                    ld        a,(de)                        ;[80d0] 1a
                    sbc       (hl)                          ;[80d1] 9e
                    inc       de                            ;[80d2] 13
                    inc       hl                            ;[80d3] 23
                    ld        a,(de)                        ;[80d4] 1a
                    sbc       (hl)                          ;[80d5] 9e
                    jp        c,$80f0                       ;[80d6] da f0 80
                    jr        c,$80e3                       ;[80d9] 38 08
                    ex        de,hl                         ;[80db] eb
                    jr        c,$80e2                       ;[80dc] 38 04
                    ld        a,(de)                        ;[80de] 1a
                    sub       (hl)                          ;[80df] 96
                    ld        (de),a                        ;[80e0] 12
                    inc       de                            ;[80e1] 13
                    inc       hl                            ;[80e2] 23
                    ld        a,(de)                        ;[80e3] 1a
                    sbc       (hl)                          ;[80e4] 9e
                    ld        (de),a                        ;[80e5] 12
                    inc       de                            ;[80e6] 13
                    inc       hl                            ;[80e7] 23
                    ld        a,(de)                        ;[80e8] 1a
                    sbc       (hl)                          ;[80e9] 9e
                    ld        (de),a                        ;[80ea] 12
                    inc       de                            ;[80eb] 13
                    inc       hl                            ;[80ec] 23
                    ld        a,(de)                        ;[80ed] 1a
                    sbc       (hl)                          ;[80ee] 9e
                    ld        (de),a                        ;[80ef] 12
                    ccf                                     ;[80f0] 3f
                    dec       b                             ;[80f1] 05
                    jp        nz,$80a1                      ;[80f2] c2 a1 80
                    jr        c,$8105                       ;[80f5] 38 0e
                    ld        a,(de)                        ;[80f7] 1a
                    rla                                     ;[80f8] 17
                    ld        (de),a                        ;[80f9] 12
                    inc       de                            ;[80fa] 13
                    ld        a,(de)                        ;[80fb] 1a
                    rla                                     ;[80fc] 17
                    ld        (de),a                        ;[80fd] 12
                    inc       de                            ;[80fe] 13
                    ld        a,(de)                        ;[80ff] 1a
                    rla                                     ;[8100] 17
                    ld        (de),a                        ;[8101] 12
                    inc       de                            ;[8102] 13
                    ld        a,(de)                        ;[8103] 1a
                    rla                                     ;[8104] 17
                    ld        (de),a                        ;[8105] 12
                    ret                                     ;[8106] c9

                    ld        de,$0000                      ;[8107] 11 00 00
                    ld        a,h                           ;[810a] 7c
                    rlca                                    ;[810b] 07
                    ret       nc                            ;[810c] d0
                    dec       de                            ;[810d] 1b
                    ret                                     ;[810e] c9

                    ld        a,h                           ;[810f] 7c
                    add       $80                           ;[8110] c6 80
                    ld        b,a                           ;[8112] 47
                    ld        a,d                           ;[8113] 7a
                    add       $80                           ;[8114] c6 80
                    cp        b                             ;[8116] b8
                    ccf                                     ;[8117] 3f
                    jp        nz,$8121                      ;[8118] c2 21 81
                    ld        a,e                           ;[811b] 7b
                    cp        l                             ;[811c] bd
                    ccf                                     ;[811d] 3f
                    jp        $8121                         ;[811e] c3 21 81
                    ld        hl,$0000                      ;[8121] 21 00 00
                    ret       nc                            ;[8124] d0
                    inc       hl                            ;[8125] 23
                    ret                                     ;[8126] c9

                    ld        hl,$85d4                      ;[8127] 21 d4 85
                    push      hl                            ;[812a] e5
                    ld        a,$01                         ;[812b] 3e 01
                    call      $8596                         ;[812d] cd 96 85
                    pop       bc                            ;[8130] c1
                    ld        hl,$8001                      ;[8131] 21 01 80
                    push      hl                            ;[8134] e5
                    call      $8162                         ;[8135] cd 62 81
                    pop       bc                            ;[8138] c1
                    ex        de,hl                         ;[8139] eb
                    ld        hl,$85e7                      ;[813a] 21 e7 85
                    push      de                            ;[813d] d5
                    push      hl                            ;[813e] e5
                    ex        de,hl                         ;[813f] eb
                    push      hl                            ;[8140] e5
                    jr        c,$8147                       ;[8141] 38 04
                    nop                                     ;[8143] ed e5
                    ld        a,$03                         ;[8145] 3e 03
                    call      $8596                         ;[8147] cd 96 85
                    pop       bc                            ;[814a] c1
                    pop       bc                            ;[814b] c1
                    pop       bc                            ;[814c] c1
                    pop       bc                            ;[814d] c1
                    jp        $8131                         ;[814e] c3 31 81
                    ret                                     ;[8151] c9

                    pop       bc                            ;[8152] c1
                    pop       hl                            ;[8153] e1
                    push      hl                            ;[8154] e5
                    push      bc                            ;[8155] c5
                    ld        a,l                           ;[8156] 7d
                   [b] call      $00d0 [/b]                        ;[8157] cd d0 00
                    ret                                     ;[815a] c9

                    [b]call      $00fb[/b]                         ;[815b] cd fb 00
                    ld        l,a                           ;[815e] 6f
                    ld        h,$00                         ;[815f] 26 00
                    ret                                     ;[8161] c9

                    pop       bc                            ;[8162] c1
                    pop       de                            ;[8163] d1
                    push      de                            ;[8164] d5
                    push      bc                            ;[8165] c5
                    ld        hl,$ffff                      ;[8166] 21 ff ff
                    inc       de                            ;[8169] 13
                    inc       de                            ;[816a] 13
                    ld        a,(de)                        ;[816b] 1a
                    and       a                             ;[816c] a7
                    ret       z                             ;[816d] c8
                    
.
.
.

Please assist.

Thanks, Andrew
User avatar
dom
Well known member
Posts: 2214
Joined: Sun Jul 15, 2007 10:01 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by dom »

If you add -m to the compile line then the map file will be generated which will help with the disassembly. I can definitely recognise the bits of the disassembly even without labels.

You've configured both your code and bss sections to the same address. One of the first things the crt does (here at 0x8026) is to clear the memory at 0x8000 - so it'll actually wipe out the loop that's clearing the memory! If you just omit `-pragma-define:CRT_ORG_BSS=0x8000` it'll automatically place BSS at the end of the code.
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Hi Dom,

I will try it at lunchtime today, although it is tempting to take a break from my job and try it now :-) .

p.s. Sorry I forgot to put a reference to your work and it was too late to edit it, so I am putting it now for others viewing the post:

Reference:
https://github.com/z88dk/z88dk/wiki/Classic--Homebrew

Thanks,Andrew
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Thanks, Dom, it fixed the structure of the RAM code but I am not getting it working (it just seems to print a bunch of CRLFs to Teraterm), but I will stare at the code for a while. At least it doesn't reboot the computer this time.

Compile Line:

Code: Select all

zcc +z80 -clib=8085 world2.c mon2.c -pragma-define:CRT_ORG_CODE=0x8000 -pragma-define:REGISTER_SP=0xFFFE -create-app -m
Fragment of a.rom file:

Code: Select all

z88dk-dis -o 0x8000 -x a.map a.rom
start:
                    ld        sp,$fffe                      ;[8000] 31 fe ff
                    ld        hl,$ffc0                      ;[8003] 21 c0 ff
                    add       hl,sp                         ;[8006] 39
                    ld        sp,hl                         ;[8007] f9
                    call      crt0_init_bss                 ;[8008] cd 25 80
                    ld        hl,$0000                      ;[800b] 21 00 00
                    add       hl,sp                         ;[800e] 39
                    ld        (exitsp),hl                   ;[800f] 22 56 86
                    call      _main                         ;[8012] cd 27 81
cleanup:
                    call      crt0_exit                     ;[8015] cd 57 80
crt_exit_loop_forever:
                    halt                                    ;[8018] 76
                    jp        crt_exit_loop_forever         ;[8019] c3 18 80
l_dcal:
                    jp        (hl)                          ;[801c] e9
__printf_format_table:
                    ld        h,h                           ;[801d] 64
                    daa                                     ;[801e] 27
                    add       d                             ;[801f] 82
                    ld        h,e                           ;[8020] 63
                    dec       de                            ;[8021] 1b
                    add       d                             ;[8022] 82
                    nop                                     ;[8023] 00
__printf_format_table64:
                    nop                                     ;[8024] 00
crt0_init_bss:
                    xor       a                             ;[8025] af
                    ld        hl,$85f0                      ;[8026] 21 f0 85
                    ld        bc,$006b                      ;[8029] 01 6b 00
                    inc       b                             ;[802c] 04
                    inc       c                             ;[802d] 0c
init_8080:
                    ld        (hl),a                        ;[802e] 77
                    inc       hl                            ;[802f] 23
                    dec       c                             ;[8030] 0d
                    jp        nz,init_8080                  ;[8031] c2 2e 80
                    dec       b                             ;[8034] 05
                    jp        nz,init_8080                  ;[8035] c2 2e 80
                    ld        (exitcount),a                 ;[8038] 32 58 86
                    ld        hl,$85f2                      ;[803b] 21 f2 85
                    ld        (hl),$13                      ;[803e] 36 13
                    ld        hl,$85fc                      ;[8040] 21 fc 85
                    ld        (hl),$15                      ;[8043] 36 15
                    ld        hl,$8606                      ;[8045] 21 06 86
                    ld        (hl),$15                      ;[8048] 36 15
                    ld        hl,$85f0                      ;[804a] 21 f0 85
                    ld        de,$865b                      ;[804d] 11 5b 86
                    ld        bc,$0001                      ;[8050] 01 01 00
                    call      asm_memcpy                    ;[8053] cd b9 85
                    ret                                     ;[8056] c9

crt0_exit:
                    ret                                     ;[8057] c9
                    .
                    .
                    .
Andrew
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Endless Loop?

Code: Select all

_main:
                    ld        hl,$85d4                      ;[8127] 21 d4 85
                    push      hl                            ;[812a] e5
                    ld        a,$01                         ;[812b] 3e 01
                    call      printf                        ;[812d] cd 96 85
                    pop       bc                            ;[8130] c1
i_2:
                    ld        hl,$85f0                      ;[8131] 21 f0 85
                    push      hl                            ;[8134] e5
                    call      _fgetc                        ;[8135] cd 62 81
                    pop       bc                            ;[8138] c1
                    ex        de,hl                         ;[8139] eb
                    ld        hl,$85e7                      ;[813a] 21 e7 85
                    push      de                            ;[813d] d5
                    push      hl                            ;[813e] e5
                    ex        de,hl                         ;[813f] eb
                    push      hl                            ;[8140] e5
                    jr        c,$8147                       ;[8141] 38 04
                    nop                                     ;[8143] ed e5
                    ld        a,$03                         ;[8145] 3e 03
                    call      printf                        ;[8147] cd 96 85
                    pop       bc                            ;[814a] c1
                    pop       bc                            ;[814b] c1
                    pop       bc                            ;[814c] c1
                    pop       bc                            ;[814d] c1
                    jp        i_2                           ;[814e] c3 31 81
.
.
.
Andrew
User avatar
dom
Well known member
Posts: 2214
Joined: Sun Jul 15, 2007 10:01 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by dom »

Add the -m8085 flag to z88dk-dis - that jr is one of the 8085 opcodes.

The jp i_2 is your while loop.
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Hi

Thanks.

Andrew.
User avatar
dom
Well known member
Posts: 2214
Joined: Sun Jul 15, 2007 10:01 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by dom »

Actually, there is something odd going on there...

I get this:

Code: Select all

i_2:
                    ld        hl,$85e1                      ;[813b] 21 e1 85
                    push      hl                            ;[813e] e5
                    call      ___fgetc                      ;[813f] cd 59 81
                    pop       bc                            ;[8142] c1
                    ex        de,hl                         ;[8143] eb
                    ld        hl,$85d8                      ;[8144] 21 d8 85
                    push      de                            ;[8147] d5
                    push      hl                            ;[8148] e5
                    ex        de,hl                         ;[8149] eb
                    push      hl                            ;[814a] e5
                    push      hl                            ;[814b] e5
                    ld        a,$03                         ;[814c] 3e 03
                    call      printf                        ;[814e] cd 86 85
                    pop       bc                            ;[8151] c1
                    pop       bc                            ;[8152] c1
                    pop       bc                            ;[8153] c1
                    pop       bc                            ;[8154] c1
                    jp        i_2                           ;[8155] c3 3b 81
Which makes me wonder what version of z88dk you're using.
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Hi Dom,

I can kind of see the program flow now to putc_cons and getc_cons. Here is my version of z88dk:

z88dk-win32-2.3

Andrew
User avatar
dom
Well known member
Posts: 2214
Joined: Sun Jul 15, 2007 10:01 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by dom »

There’s been some improvements to 8085 support sonce 2.3 so I’d recommend using a nightly - at least then our code would match up!
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Thanks, Dom, willdo. Andrew
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Hi Dom,

I am now using the latest z88dk build(Sept 20, 2024). I get some output this time but its weird. I will try to examine the code some more. Ealier I put all the assmbly into an external file(hardware.asm) and output worked but input didn't (according to your article). I added 10 crlfs to world2.c below in case of framing errors but i got this:

Teraterm Output

Code: Select all

**** WELCOME TO 8085 BASIC V1 ****
32K ROM, 32K RAM SYSTEM, 32768 BASIC BYTES FREE
READY.
abc123
abc123
11111111111111111111111111111111111111m11111111
world2.c

Code: Select all

#include <stdio.h>

int main()
{
    for(int i = 0; i < 10; i++) {
        printf("\r\n");
    } 
    printf("Hello from z88dk!\n");

    while ( 1 ) {
        int c = getchar();
        printf("<%c>=%d ", c,c);
    }
}

a.rom fragment

Code: Select all

_main:
                    ld        hl,$0000                      ;[8124] 21 00 00
                    push      hl                            ;[8127] e5
                    jp        i_4                           ;[8128] c3 2e 81
i_2:
                    pop       hl                            ;[812b] e1
                    inc       hl                            ;[812c] 23
                    push      hl                            ;[812d] e5
i_4:
                    pop       hl                            ;[812e] e1
                    push      hl                            ;[812f] e5
                    ld        a,l                           ;[8130] 7d
                    sub       $0a                           ;[8131] d6 0a
                    ld        a,h                           ;[8133] 7c
                    rla                                     ;[8134] 17
                    ccf                                     ;[8135] 3f
                    rra                                     ;[8136] 1f
                    sbc       $80                           ;[8137] de 80
                    jp        nc,i_3                        ;[8139] d2 49 81
                    ld        hl,$85f2                      ;[813c] 21 f2 85
                    push      hl                            ;[813f] e5
                    ld        a,$01                         ;[8140] 3e 01
                    call      printf                        ;[8142] cd b4 85
                    pop       bc                            ;[8145] c1
                    jp        i_2                           ;[8146] c3 2b 81
i_3:
                    pop       bc                            ;[8149] c1
                    ld        hl,$85f5                      ;[814a] 21 f5 85
                    push      hl                            ;[814d] e5
                    ld        a,$01                         ;[814e] 3e 01
                    call      printf                        ;[8150] cd b4 85
                    pop       bc                            ;[8153] c1
i_5:
                    ld        hl,$8611                      ;[8154] 21 11 86
                    push      hl                            ;[8157] e5
                    call      ___fgetc                      ;[8158] cd 86 81
                    pop       bc                            ;[815b] c1
                    push      hl                            ;[815c] e5
                    ld        hl,$8608                      ;[815d] 21 08 86
                    push      hl                            ;[8160] e5
                    ld        de,sp+$02                     ;[8161] 38 02
                    ld        hl,(de)                       ;[8163] ed
                    push      hl                            ;[8164] e5
                    ld        de,sp+$04                     ;[8165] 38 04
                    ld        hl,(de)                       ;[8167] ed
                    push      hl                            ;[8168] e5
                    ld        a,$03                         ;[8169] 3e 03
                    call      printf                        ;[816b] cd b4 85
                    pop       bc                            ;[816e] c1
                    pop       bc                            ;[816f] c1
                    pop       bc                            ;[8170] c1
                    pop       bc                            ;[8171] c1
                    jp        i_5                           ;[8172] c3 54 81
i_6:
                    ret 
      .
      .
      .              
                    
                    
                     
Andrew
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Hi Dom,

I have an idea why its not working. I am calling my CIN routine via getchar in the c code without call BRID (baud rate idendication routine) from the Intel Serial ap-29 application note, which corrects the value of halfbit (my bittime is ok) and also, how does the code suddenly know where to find these variables: halfbit,bittime? Maybe that is why it is not displaying the Hello message and showing the value of the inputted character. I will edit the code and try it again later, when I have a chance.

Andrew
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Adding BRID, does nothing, It is still not working, It just reboots sometime when I press a character.

mon2.c

Code: Select all


#include <stdio.h>

// Existing I/O functions

int fputc_cons_native(char c) __naked
{
__asm
    pop     b          ; Pop return address into BC
    pop     h          ; Pop character to print into HL (L contains the character)
    push    h          ; Push HL back onto the stack
    push    b          ; Push BC back onto the stack
    MOV     a, l       ; Move the character from L to accumulator A
    CALL    0x00D0      ; Call COUT routine at address 0x00D0 (replace if different)
    RET                 ; Return to caller
__endasm;
} 


int fgetc_cons() __naked
{
__asm
    CALL    0x00FB      ; Call CIN routine at address 0x00FB (replace if different)
    MOV     l, a        ; Move the received character from accumulator A to L
    MVI     h,0         ; Clear H to set HL to 0x00XX (where XX is the character)
    RET                 ; Return to caller with character in HL
__endasm;
}

// New function to call BRID at address 0x0073
void brid_call() __naked
{
__asm
    CALL    0x00A1      ; Call BRID routine located at address 0x0073
    RET                 ; Return to caller
__endasm;
}
world2.c

Code: Select all


#include <stdio.h>

// Declare the brid_call function from mon2.c
extern void brid_call(void);

int main()
{
    // Call BRID once at the start
    brid_call();

    // Clear the screen by sending 10 CRLF sequences
    for(int i = 0; i < 10; i++) {
        printf("\r\n");
    } 
    printf("Hello from z88dk!\n");

    // Enter an infinite loop to continuously read and echo input characters
    while (1) {
        int c = getchar();            // Read a character from input
        printf("<%c>=%d ", c, c);     // Echo the character back with its ASCII value
    }
}
Andrew
User avatar
dom
Well known member
Posts: 2214
Joined: Sun Jul 15, 2007 10:01 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by dom »

I'm travelling again so I've been and will be a for a few more days, mostly AFK.

I've slightly lost track of where we are, but I think it's in this state:

1. When compiled to ROM everything works
2. When compiled to RAM and loaded alongside a firmware things break

I've obviously not got any visibility on what the firmware is doing, but I presume it must use some RAM for variables/buffers - do they overlap with where the program is being loaded?

Random crashes are usually memory/stack related - how does it behave if you recompile the RAM to org at $c000?
User avatar
feilipu
Member
Posts: 54
Joined: Tue Nov 15, 2016 5:02 am

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by feilipu »

Out of interest, where are the working variables for your Basic stored?

In my version the space from 0x8000 to 0x8400 is used, so the lowest feasible origin is 0x8400. By default the origin is set to 0x9000 (historical reasons that aren’t relevant here).
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Thanks Dom and Feilipu, I already accounted for memory / stack overlap.
At this point i am giving up for now since I spent too much time on this.

Here are the details anyways.

Memory Map

0x0000->0x7FFF ROM , NVRAM with WE tied high to protect it.
0x8000->0xFFFF RAM

My goal was to use a high level language to write/port code for the 8085, hence I started to write a BASIC for it, I had tried earlier, the Tiny Basic from here: https://www.nutsvolts.com/magazine/arti ... r-computer , and it worked, but it was too limiting, so then later I tried to use a c -> 8085 cross assembler, hence why I am here...


stack pointer -> 0xFFFE
Variables ->0x200 (after the rom program)
User Input: 0xA000 (moved from 0x8000) so it stopped rebooting, but showed unexpected behaviour mentioned already in this thread.

RAM program deployed to 0x8000.

p.s. I used the 8085 to make a SBC due to on board clock and serial port, but I didn't realize just how little software was available for it. In hindsight, I should have used a z80 (maybe i will switch)

Andrew
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

However, using the method where the monitor assembly code is in an external ASM file a la Dom
's article: https://github.com/z88dk/z88dk/wiki/Classic--Homebrew , worked for me with char output (but not input), maybe i will pursue this later...

Andrew.
User avatar
dom
Well known member
Posts: 2214
Joined: Sun Jul 15, 2007 10:01 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by dom »

Do you mind sharing your project with me via email - I can adjust ticks to emulate your environment and get to the bottom of the issue.
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Hi Dom, I am trying one more thing via the hardware.asm method and then if it doesn't work, I will email you.

Andrew
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Hi Dom,

It didn't work. I see I can send emails through this site. I appreciate all the help. Can you please provide your email address or instructions on contacting you privately through the site?

Thanks, Andrew
User avatar
dom
Well known member
Posts: 2214
Joined: Sun Jul 15, 2007 10:01 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by dom »

Sure, just email dom at z88dk.org and it'll get through to me
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Thanks, I assumed the hardware.asm external file method in the files I sent you, but it doesn't have to use that method, its just the last thing I tried...

Andrew
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Hi All, It is resolved now.

Details are below:

Memory Map

0000H START OF ROM
0200H START OF VARIABLES
7FFFH END OF ROM

8000H START OF RAM PROGRAM
B000H START OF INPUT BUFFER FROM MONITOR
FFFEH STACK POINTER
FFFFH END OF RAM

Compile Command

Code: Select all

zcc +z80 -clib=8085 world.c monitor.c -pragma-define:CRT_ORG_CODE=0x8000 -pragma-define:REGISTER_SP=0xFFFE -m -create-app
World.c, prints some chars and gets a char and prints its ascii value, continues forever

Code: Select all

#include <stdio.h>

int main()
{
      
    // Clear the screen by sending 10 CRLF sequences
    for(int i = 0; i < 10; i++) {
        printf("\r\n");
    } 

    printf("Hello from z88dk!\n");

    while ( 1 ) {
        int c = getchar();
 // Clear the screen by sending 10 CRLF sequences
    for(int i = 0; i < 10; i++) {
        printf("\r\n");
    } 
        printf("<%c>=%d ", c,c);
    }
}
Monitor.c, assembly routines for sending and receiving chars to / from the monitor to the world.c program
printf and getchar will automatically use these routines

Code: Select all

#include <stdio.h>

int fputc_cons_native(char c) __naked
{
__asm
    pop     b  ;return address
    pop     h  ;character to print in l
    push    h
    push    b
    MOV      a,l
    MOV c,a ; routine below expects the character to be sent in the C register.
    call    $009B ; sends a character
    ret
__endasm;
} 

int fgetc_cons() __naked
{
__asm
    call    $00C6   ;gets a character and returns it into the accumulator.
    mov     l,a     ;return the result in hl
    mvi     h,0
    ret
__endasm;
}

Teraterm Output
The welcome section and echo'd characters are from my monitor on ROM, the rest of the output is from the compiled c program with z88dk on RAM.

**** WELCOME TO 8085 BASIC V1 ****
32K ROM, 32K RAM SYSTEM, 32768 BASIC BYTES FREE
READY.
ABC123
ABC123










Hello from z88dk!
S









<S>=83 A









<A>=65 P









<P>=80 ~









<~>=126 @









<@>=64

Thanks, All,
Andrew
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: In 8085 sbc, Monitor in ROM, compiled z88dk prog in Ram not working

Post by andrewec »

Addendum, in world.c above in the getchar section, probably its only necessary to send the 10 clfs one time prior to printing due to re-entering the monitor, not every time like I have it above... In fact you should first try it without the sending of the crlf errors, but If you get framing errors, add them back in.

Andrew
Post Reply