RetroBrew Computers Forum
Discussion forum for the RetroBrew Computers community.

Home » RBC Forums » General Discussion » need help with 8051 macro assembler
need help with 8051 macro assembler [message #10529] Tue, 05 December 2023 15:02 Go to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
Hi
I am working on a SAB80535 dev board and would like to port Tiny BASIC to it. The PJRC 8051 dev board has Tiny BASIC here:

https://www.pjrc.com/tech/8051/contrib/tb51/index.html

the Intel Hex file does not properly on my SAB80535 because the memory map is different than the PJRC 8051 dev board.

The PJRC 8051 dev board has RAM at 0x0000 to 07FFF and ROM at 0x8000 to 0xF7FF.

However, my SAB80535 relies on external ROM only it has ROM at 0x0000 to 0x7FFFF and RAM at 0x8000 to 0xF7FF

When I run the Tiny BASIC executable on my SAB80535 dev board, it seems to work but when you "LIST" a simple program, Tiny BASIC doesn't seem to remember the program you just entered.

Looking at the tb51.asm, I think the reason is obvious; it is trying to store program data and variables in the RAM area, which is actually ROM on my board, and it is not getting recorded.

The solution should be to adjust the tb51.asm code but I am not very familiar with Macro Assembler and that's where I need some help.

I found a Macro Assembler for 8051 here:

http://plit.de/asem-51/home.htm

If I comment out the first line "$MOD52", then it assembles clean with no errors but the resulting executable behaves the same as before. If I change the lines

EXTRAM EQU 2034H ;External program buffer begins after 26 vars.
RAMLIM EQU 3000H ;Allowance made for 4K RAM buffer.

to

EXTRAM EQU 0A034H ;External program buffer begins after 26 vars.
RAMLIM EQU 0B000H ;Allowance made for 4K RAM buffer.

Then the Tiny BASIC will at least remember a program I type in. However, I think there is another problem at this line:

DSEG

ORG 20H

I think it is trying to store data over the ROM location at 0x0020. Somehow, I need to find a way to move this data segment into RAM at 0xC000. There is a similar problem here at this line:

ORG 30H

Does someone know Macro Assembler well enough to help me move the data segments to the RAM region at 0xC000? Better yet, store it at the end of the Tiny BASIC code at 0x8000 in a special data segment?

Thanks in advance for any help. Much appreciated. Andrew Lynch
Re: need help with 8051 macro assembler [message #10533 is a reply to message #10529] Wed, 06 December 2023 04:01 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
Hi
Did some more research on what Tiny BASIC is doing and why. It turns out when it initializes the data segment (DSEG), it is putting flags in the bit-addressable internal RAM (20H) and some variables in the register banks (30H). The SAB80535 supports the functionality and also placing the stack pointer at 7FH to use the upper 128 bytes of internal RAM. I don't think the DSEG is the problem. On the PJRC board, Tiny BASIC uses some functionality specific to the 8052 instead of just the 8051 but the SAB80535 seems to support the extended 8052 functions also. In short, I don't think the DSEG is the problem.

The author ported Tiny BASIC to the PJRC design which has ROM at 0x8000 to 0xF7FF and RAM at 0x0000 to 0x7FFF (plus the internal ROM 0x0000 to 0x1FFF). The SAB80535 dev board reverses the memory map ROM and RAM locations and I think that might be confusing Tiny BASIC even when the system configuration constants are adjusted

; GLOBAL VARIABLE AND DATA STRUCTURE DECLARATIONS:
; ====== ======== === ==== ========= ============
;
; Intended System Configuration Constants:
;
EXTRAM EQU 2034H ;External program buffer begins after 26 vars.
RAMLIM EQU 3000H ;Allowance made for 4K RAM buffer.
EXTROM EQU 9080H ;Start of external ROM space.

There is another possibility that switching to ASEM-51 from the original Metalink-51 macro assembler is creating a different object code based on similar assembler input. That's easy enough to test and I'll do that tonight first thing. Just keep everything the same except for a the minimal changes necessary to get it to assemble with ASEM-51 and then compare the resulting binary image to the distribution original. It will be interesting if there are differences introduced just by the assembler.

Any ideas? Thanks, Andrew Lynch

Re: need help with 8051 macro assembler [message #10534 is a reply to message #10533] Wed, 06 December 2023 06:28 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
Hi
I confirmed that the ASEM-51 and MetaLink macro assemblers produce the exact same binaries assuming only the minimal changes necessary to assemble. The first line in the tb51.asm file is an assembler directive "$mod51" which the MetaLink supports but the ASEM-51 requires two lines "$nomod51" and "$include (80515.mcu)" as its equivalent. With that minimal change (no corrections to System Configuration Constants) both produce identical binaries so I think we can rule out code changes introduced by ASEM-51.

Thanks, Andrew Lynch
Re: need help with 8051 macro assembler [message #10542 is a reply to message #10534] Fri, 08 December 2023 14:29 Go to previous messageGo to next message
trianon is currently offline  trianon
Messages: 20
Registered: May 2020
Junior Member
Hi,

Been many years I used to program the SAB80517A (upgrade version of the 535) in assembler, some things that come to mind :

The 8051 processor a Harvard architecture, meaning it has 64K code space & 64K data space ! (And 256 bytes internal RAM, first 128bytes are R0~R7 over 4 banks + some bits & last 128b as stack or indirect addressing.Wink

The code starts at $8000 in the assembler :

CSEG
ORG 8000H
JMP S_INIT ;Jump to system initialization routine.

You can change this to $0000 if you want to store it in flash/eeprom at $0000, I know some cheap development boards don't decode A15 so code compiled for $8000 will also run like it's in $0000.


I have not fully check all source code, but I found already :

This code below puts the high byte of the address fixed on P2 port = 8 bit high address port


STREXT: MOV R1,A
DD001: MOV P2,#HIGH(EXTRAM)
MOV A,TMP0
MOVX @R1,A
INC R1 ;Bump pointers.
MOV A,TMP1 ;Move high-order byte into variable array.
MOVX @R1,A
RET

Same happens during memory init :

RAM_INIT:
CLR A ;Many bytes to be cleared...
MOV MODE,A ;Interactive mode, decimal radix.
MOV FLAGS,A ;Interroutine flags.
DD010: MOV P2,#HIGH(EXTRAM);Select first External RAM page.
MOV R0,A
MOV A,#5AH ;Random bit pattern.
MOVX @R0,A
MOVX A,@R0
XRL A,#5AH
JZ EXTINI
CLR A
MOV R0,#US_VAR ;Clear variable array.
INIT_1: MOV @R0,A
INC R0
CJNE R0,#US_VAR+2*NO_VAR,INIT_1 ;Loop until all vars cleared.
SJMP INIT_3


This means you need to change
EXTRAM EQU 2034H
to
EXTRAM EQU 8034H



This is the code to start at the first line of code, changing EXTRAM should also work here.
;REWIND
; Reset Cursor to start of current program buffer space.
;
REWIND: CLR CHAR_FLG
JB ROMMOD,REWROM
MOV PNTR_H,#HIGH(EXTRAM)
MOV PNTR_L,#LOW(EXTRAM)
RET


What to look for in source code is :
MOVC is to read data from CODE memory
MOVX is to read or write data from/to RAM


And if this is not strange enough (compared to 68000) on may development boards they added logic to have the RAM access possible in both code and RAM.
For example EEPROM at $0000 so it will boot and RAM at $8000 at both code and data, this way the bootloader could download hex files over serial port,
store it in the RAM by MOVX. Once all hex files are stored, you can run it out of ram because.
If this additional logic (PSEN and RD) would not be implemented you could not run code out of RAM.

Re: need help with 8051 macro assembler [message #10543 is a reply to message #10542] Sat, 09 December 2023 18:07 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
Hi
Thank you! I will give that a try first thing tomorrow. Much appreciated!

Andrew Lynch
Re: need help with 8051 macro assembler [message #10544 is a reply to message #10543] Sun, 10 December 2023 10:13 Go to previous message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
Hi
Experimenting some more with Tiny BASIC on the SAB80535 dev board. I tried changing EXTRAM to 8034H but no luck. With that change, tiny BASIC does not start when I jump to it from PAULMON2.

I have had some luck with the changes I've made below

Lines 1-9, I commented out line 1 and added 7 and 8

; $MOD52
$NODEBUG
$PAGEWIDTH (120)
$PAGELENGTH (66)
$TITLE (Tiny-Basic51 - Modified for Metalink ASM51)

$NOMOD51
$INCLUDE (80515.MCU)




Then I made these changes to lines 194, 195, and 196

EXTRAM EQU 2034H+7000H ;External program buffer begins after 26 vars.
RAMLIM EQU 3000H+7000H ;Allowance made for 4K RAM buffer.
EXTROM EQU 9080H+3000H ;Start of external ROM space.


I added 7000H to EXTRAM and RAMLIM pushing those deeper into RAM space. Tiny BASIC itself occupies between 8000H and 8FFFH approximately according to the LST file.


When I start the SAB80535 dev board and upload the Tiny BASIC hex file to the RAM (8000-F7FF) I get this:

Welcome to PAULMON2 v2.1, by Paul Stoffregen

See PAULMON2.DOC, PAULMON2.EQU and PAULMON2.HDR for more information.


Program Name Location Type
List 1000 External command
Single-Step 1400 External command
Memory Editor (VT100) 1800 External command

PAULMON2 Loc:2000 >
PAULMON2 Loc:2000 >
PAULMON2 Loc:2000 > New location

New memory location: 8000

PAULMON2 Loc:8000 > Download

Begin ascii transfer of Intel hex file, or ESC to abort


Download completed

Summary:
202 lines received
3202 bytes received
3202 bytes written
No errors detected


PAULMON2 Loc:8000 >
PAULMON2 Loc:8000 > Jump to memory location

Jump to memory location (8000), or ESC to quit: 8000

running program:


MCS-51 TINY BASIC/Metalink-Compatible Source V2.3
OK
>


So far, so good. It looks like it is working. So I type in a simple program, list it, and run it

OK
>10 I=5
>20 PRINT "HELLO WORLD", I
>LIST
10 I=5
20 PRINT "HELLO WORLD", I
OK
>RUN
HELLO WORLD 5
WHAT?
OK
>

but notice I get the WHAT? indicating some kind of error. I can add more to the program like this


OK
>10 I=5
>20 PRINT "HELLO WORLD", I
>LIST
10 I=5
20 PRINT "HELLO WORLD", I
OK
>RUN
HELLO WORLD 5
WHAT?
OK
>


It looks like it is mostly working but for some reason some variable is getting corrupted or not stored properly in the first place. It is so close to working but just not quite.

Notice, even if I delete the variable and just leave the regular program statements, I still get the WHAT? error


OK
>LIST
10 I=5
20 PRINT "HELLO WORLD", I
30 I=I+1
40 GOTO 20
OK
>10
>30
>LIST
20 PRINT "HELLO WORLD", I
40 GOTO 20
OK
>RUN
HELLO WORLD 5
WHAT?
OK
>


Any ideas and/or suggestions? Thanks, Andrew Lynch


PS, I've had a major breakthrough event, I noticed line 321 is "SETB ROMMOD" so I commented it out and replaced it with "CLR ROMMOD" and now the Tiny BASIC sample program runs just fine. The funny thing is the code in the RUNROM subroutine and there doesn't appear to be any obvious way for that routine to get executed. It looks like dead code but apparently not. RUNROM is only called from one line, and it is commented out. The line above it is a JMP instruction so how the MCU is getting to that routine in quite mysterious. But it works so I'll take it!




  • Attachment: tb51a.asm
    (Size: 63.82KB, Downloaded 120 times)
  • Attachment: tb51.asm
    (Size: 63.77KB, Downloaded 128 times)
  • Attachment: tb51a.lst
    (Size: 145.67KB, Downloaded 121 times)

[Updated on: Sun, 10 December 2023 16:31]

Report message to a moderator

Previous Topic: Muntz65, a bare bone 25MHz 6502 computer with W65C02, RAM, and 22V10
Next Topic: BB80 (previously Muntz80), Bare Bones Z80 computer


Current Time: Tue Jul 15 12:51:04 PDT 2025

Total time taken to generate the page: 0.00783 seconds