Why do I get this text instead of the text in upper left corner?

Do you live and breathe hexadecimal? Do you like speaking to hardware directly?

Moderator: Programming Moderators

TheSwedishLord
Posts: 17
Joined: Mon Aug 17, 2020 7:47 am

Why do I get this text instead of the text in upper left corner?

Postby TheSwedishLord » Sat Aug 29, 2020 3:56 pm

Image
https://easyupload.io/f0hr5q

Why is that happening, forgetting something?
Using CSpect to emulate Next

Code looks like this

Code: Select all

org  $8000

Start:
        call ROM_CLS
        ld de, string
        ld bc, stringlen
        call ROM_PRINT
        ret

string db AT, 10, 2, INK, 2, "Test"
stringlen equ $-string

;
; ROM routine addresses
;
ROM_CLS                 EQU  0x0DAF             ; Clears the screen and opens channel 2
ROM_OPEN_CHANNEL        EQU  0x1601             ; Open a channel
ROM_PRINT               EQU  0x203C             ; Print a string 
;
; PRINT control codes - work with ROM_PRINT and RST 0x10
;
INK                     EQU 0x10
PAPER                   EQU 0x11
FLASH                   EQU 0x12
BRIGHT                  EQU 0x13
INVERSE                 EQU 0x14
OVER                    EQU 0x15
AT                      EQU 0x16
TAB                     EQU 0x17
CR                      EQU 0x0C

Ped7g
Posts: 256
Joined: Mon Jul 16, 2018 7:11 pm

Re: Why do I get this text instead of the text in upper left corner?

Postby Ped7g » Sat Aug 29, 2020 5:22 pm

Who knows. The code + build + launch info is incomplete, I have no idea what is your output format, how you start the code, and how you start the CSpect to run this code.

All of these could cause major difference in output you will get (from "reset" of Spectrum to white screen with nothing visible).

As you are calling ROM routines, the outcome depends on how well you do preserve the system variables of NextZXOS/BASIC, and whether you did start full NextZXOS, or you are launching some SNA/SNX/NEX file directly in CSpect in the mode without NextZXOS emulation (and without proper system variables, so the ROM routines may have unexpected results).

Ped7g
Posts: 256
Joined: Mon Jul 16, 2018 7:11 pm

Re: Why do I get this text instead of the text in upper left corner?

Postby Ped7g » Sat Aug 29, 2020 5:33 pm

If I modify your source code for sjasmplus for ZXSPECTRUM48 device, and save it as zx48 snapshot, with the fake system variables (killing real NextZXOS ones, in case you launch such snapshot on real Next), and I launch such snapshot directly in CSpect without full NextZXOS emulation, I get this:
test.png
test.png (7.17 KiB) Viewed 2294 times
Basically works as the code said, also returns to 48k BASIC, but with interrupts disabled, so there's no way to control it any more.

If I would modify that code to become NEX file and start it directly without full NextZXOS, it would probably "restart" after the `ret` as the system is not in valid state to return to it (too lazy to actually try it, just mentioning as illustration, that the final result depends a lot on what you are precisely doing).

dave18
Posts: 186
Joined: Tue May 30, 2017 1:06 am
Location: Bristol, UK

Re: Why do I get this text instead of the text in upper left corner?

Postby dave18 » Sat Aug 29, 2020 6:17 pm

Why not use CSpect's debug feature to find out what the code does? Also, there's every chance it is drawing the text properly but then the RET returns and runs some random code to corrupt the display.

TheSwedishLord
Posts: 17
Joined: Mon Aug 17, 2020 7:47 am

Re: Why do I get this text instead of the text in upper left corner?

Postby TheSwedishLord » Sun Aug 30, 2020 8:48 am

Ped7g wrote:
Sat Aug 29, 2020 5:22 pm
Who knows. The code + build + launch info is incomplete, I have no idea what is your output format, how you start the code, and how you start the CSpect to run this code.

All of these could cause major difference in output you will get (from "reset" of Spectrum to white screen with nothing visible).

As you are calling ROM routines, the outcome depends on how well you do preserve the system variables of NextZXOS/BASIC, and whether you did start full NextZXOS, or you are launching some SNA/SNX/NEX file directly in CSpect in the mode without NextZXOS emulation (and without proper system variables, so the ROM routines may have unexpected results).
This is how the file is created

Code: Select all

;;
;; Set up the Nex output
;;

        ; This sets the name of the project, the start address, 
        ; and the initial stack pointer.
        SAVENEX OPEN "project.nex", Start, $ff40

        ; This asserts the minimum core version.  Set it to the core version 
        ; you are developing on.
        SAVENEX CORE 2,0,0

        ; This sets the border colour while loading (in this case white),
        ; what to do with the file handle of the nex file when starting (0 = 
        ; close file handle as we're not going to access the project.nex 
        ; file after starting.  See sjasmplus documentation), whether
        ; we preserve the next registers (0 = no, we set to default), and 
        ; whether we require the full 2MB expansion (0 = no we don't).
        SAVENEX CFG 7,0,0,0

        ; Generate the Nex file automatically based on which pages you use.
        SAVENEX AUTO
This is how CSpect is started: CSpect.exe -r -brk -tv -w3 -vsync -zxnext -nextrom -mmc=cspect-next-16gb.img

I do start the nex file from menu, is there a better way?

I know why I get the weird text now, it's running behind and try to execute bogus instructions. How do I run that file the propper way so it can exit to NextBasic after it have finished?

TheSwedishLord
Posts: 17
Joined: Mon Aug 17, 2020 7:47 am

Re: Why do I get this text instead of the text in upper left corner?

Postby TheSwedishLord » Sun Aug 30, 2020 10:02 am

Think I know why I get the weird output but on a real Next what output format should I have there?
If I want to use ZX Spectrum 48K?
If I want to use Next (.nex?)?
If I want to use ZX Spectrum 128K?

A little bit new to this emulator and how it works so if you think the questions is stupid it's probably correct and I do appreciate your help

Ped7g
Posts: 256
Joined: Mon Jul 16, 2018 7:11 pm

Re: Why do I get this text instead of the text in upper left corner?

Postby Ped7g » Sun Aug 30, 2020 12:41 pm

The question is not stupid, opposite. It's not trivial subject.

Basically you don't return to BASIC from NEX files. With current version of NEX file (V1.2) you will, if nothing else, lose the information about where was the system stack located (plus it's super easy to produce code which does overwrite vital parts of NextZXOS, unless you put extra effort into avoiding all system banks). The best way to "exit" from NEX app is to restore system configurations you did to non-obvious things (nextregisters affecting system too deeply across soft-reset, like altROM stuff, etc.. you will probably not hit any of these any time soon, if you are learning Z80 assembly and experimenting with screen/keyboard/sound) and do `nextreg 2,1` to trigger soft-reset. (BTW, this does nothing in CSpect emulator, so follow with infinite loop in code to just keep hanging in CSpect).

So NEX format is mostly targetting people taking over the machine, definitely. Like games. (and it is quite similar to snapshot formats in many ways, which is why it is rather destructive to the NextZXOS, unless you go quite some lengths to avoid that at least under common scenarios and you can not produce completely OS-safe NEX at all, any NEX file you produce, I can tamper with the NextZXOS before loading it enough to make the two clash .. but that's like academic stuff, most of the users will power on Next and load the NEX file, which makes the conditions somewhat stable and you can assume a thing or two)

Then there is DOT-command format, where the startup procedure is considerably different and returning back to OS is normal, but you have only 8kiB of space in the trivial variant, being launched from within the divMMC memory area. You can still extend your workspace by allocating memory from NextZXOS, and eventually loading further banks of code, so there are dot-commands longer than 8kiB and using more data, but you need to behave and use correct services to stay OS friendly.
- this is excellent tool mostly for command line tools, or generally utility software, where you expect as user to return back to main menu/command line.

And final common option are regular SAVE/LOAD blocks of ZX BASIC (and NextBASIC). You can collect the regular tape blocks into single TAP file to make the distribution look more compact and easier to manipulate (copying file to card, etc.). You can then in the initial small BASIC loader do the correct things like CLEAR, and LOAD further blocks of CODE or allocate banks in BASIC, etc.. ie. having regular BASIC code already running at the top level, you just do the usual ZX Spectrum stuff to incorporate any machine code into it without destroying your top level BASIC environment. - this is also IMO preferred way for modern classic ZX production, because by using the regular SAVE/LOAD commands you get package which is either directly runnable on various disk systems, or very easy to patch for some specific one of them. The other way of distributing disk images tightly coupled with particular disk system makes it difficult to run such SW on ZX without it, like for example in recent days I have seen new release of disk-mag for ZX scene distributed as MDOS D80 file (old Czech/Slovak disk system from Didaktik Skalica company) -> I can't run that on my Next (only in emulators which support these disk images).

I wouldn't underestimate the TAP form for distribution even of the Next-specific software, as TAP is long-time well defined universal format. The only downside to it is that when you launch it from NextZXOS browser, it will ask about HW configuration before loading the first part, so for Next-specific software there's extra press of "N" to select Next... I find that completely acceptable for the benefits mentioned above.

(I may sound very vague, not telling you precise instructions, but that's intentional, because I'm not sure myself, what are the precise rules, depends what you have on mind, it's quite a list and most of the things are not relevant to many use cases - plus I'm personally all about taking over the machine and not using the BASIC or OS services at all, so my experience with preserving it correctly is minimal - also I'm not aware of some thorough detailed manual for NextZXOS describing these in one place.. one part is to get familiar with NextBASIC in the first place, then there is documentation about NextZXOS service API, describing also requirements on the SW side to make them work, but I think there are few more unwritten rules, which you can accidentally break.)

Finally there are classic snapshot formats like SNA, Z80, SNX ... those are by their nature destructive to the NextZXOS, and while for example I use SNA a lot in sjasmplus to quickly prototype/check something, I'm always doing that with the knowledge I'm taking full control over machine, and destroying OS variables is ok in such case.

-----------------------------------
Now with your specific TEST example - I would do this...
Your code as is is OK-ish toward ZX BASIC. You can save it as CODE block from address 32768 (0x8000) (it's 13 bytes long if I'm counting it correctly, but you can use the assembler symbols to not calculate it manually).

Then you need BASIC loader which will prepare the NextBASIC for such code block and start the code:

Code: Select all

10 CLEAR 32767 : REM moves stack under the 32768 where your code will be loaded
20 LOAD "your_code" CODE 32768 : REM load your code into memory
30 RANDOMIZE USR 32768 : REM and start it
Now you can pack both basic loader and the code block together into TAP file, and launch it on Next, it will return back to OS normally.

The PITA part is how to produce the BASIC loader if you are primarily interested into assembler and you are working for example in sjasmplus. That one has example TapLib in distribution for similar trivial loader, and EMPTYTAP/SAVETAP directives to build TAP file directly from asm source, but it's still not completely trivial. For your custom BASIC loader you either need to understand the binary form of tokenized BASIC and re-create it by using the DB/DW statements in assembly, or "cheat" by preparing the BASIC part on the Next itself, save it into TAP file, and then start every build of assembly by copying this loader-only TAP to final file as start, then add other blocks to it by SAVETAP...

But what is your reasoning to get back to BASIC in the first place, and can't you just soft-reset your app? (with CSpect itself it's even less clear what's your plan, I usually test my code and hit escape to close the CSpect completely, never been in need to get "back into the basic").

TheSwedishLord
Posts: 17
Joined: Mon Aug 17, 2020 7:47 am

Re: Why do I get this text instead of the text in upper left corner?

Postby TheSwedishLord » Sun Aug 30, 2020 9:10 pm

Ped7g wrote:
Sun Aug 30, 2020 12:41 pm
The question is not stupid, opposite. It's not trivial subject.

Basically you don't return to BASIC from NEX files. With current version of NEX file (V1.2) you will, if nothing else, lose the information about where was the system stack located (plus it's super easy to produce code which does overwrite vital parts of NextZXOS, unless you put extra effort into avoiding all system banks). The best way to "exit" from NEX app is to restore system configurations you did to non-obvious things (nextregisters affecting system too deeply across soft-reset, like altROM stuff, etc.. you will probably not hit any of these any time soon, if you are learning Z80 assembly and experimenting with screen/keyboard/sound) and do `nextreg 2,1` to trigger soft-reset. (BTW, this does nothing in CSpect emulator, so follow with infinite loop in code to just keep hanging in CSpect).

So NEX format is mostly targetting people taking over the machine, definitely. Like games. (and it is quite similar to snapshot formats in many ways, which is why it is rather destructive to the NextZXOS, unless you go quite some lengths to avoid that at least under common scenarios and you can not produce completely OS-safe NEX at all, any NEX file you produce, I can tamper with the NextZXOS before loading it enough to make the two clash .. but that's like academic stuff, most of the users will power on Next and load the NEX file, which makes the conditions somewhat stable and you can assume a thing or two)

Then there is DOT-command format, where the startup procedure is considerably different and returning back to OS is normal, but you have only 8kiB of space in the trivial variant, being launched from within the divMMC memory area. You can still extend your workspace by allocating memory from NextZXOS, and eventually loading further banks of code, so there are dot-commands longer than 8kiB and using more data, but you need to behave and use correct services to stay OS friendly.
- this is excellent tool mostly for command line tools, or generally utility software, where you expect as user to return back to main menu/command line.

And final common option are regular SAVE/LOAD blocks of ZX BASIC (and NextBASIC). You can collect the regular tape blocks into single TAP file to make the distribution look more compact and easier to manipulate (copying file to card, etc.). You can then in the initial small BASIC loader do the correct things like CLEAR, and LOAD further blocks of CODE or allocate banks in BASIC, etc.. ie. having regular BASIC code already running at the top level, you just do the usual ZX Spectrum stuff to incorporate any machine code into it without destroying your top level BASIC environment. - this is also IMO preferred way for modern classic ZX production, because by using the regular SAVE/LOAD commands you get package which is either directly runnable on various disk systems, or very easy to patch for some specific one of them. The other way of distributing disk images tightly coupled with particular disk system makes it difficult to run such SW on ZX without it, like for example in recent days I have seen new release of disk-mag for ZX scene distributed as MDOS D80 file (old Czech/Slovak disk system from Didaktik Skalica company) -> I can't run that on my Next (only in emulators which support these disk images).

I wouldn't underestimate the TAP form for distribution even of the Next-specific software, as TAP is long-time well defined universal format. The only downside to it is that when you launch it from NextZXOS browser, it will ask about HW configuration before loading the first part, so for Next-specific software there's extra press of "N" to select Next... I find that completely acceptable for the benefits mentioned above.

(I may sound very vague, not telling you precise instructions, but that's intentional, because I'm not sure myself, what are the precise rules, depends what you have on mind, it's quite a list and most of the things are not relevant to many use cases - plus I'm personally all about taking over the machine and not using the BASIC or OS services at all, so my experience with preserving it correctly is minimal - also I'm not aware of some thorough detailed manual for NextZXOS describing these in one place.. one part is to get familiar with NextBASIC in the first place, then there is documentation about NextZXOS service API, describing also requirements on the SW side to make them work, but I think there are few more unwritten rules, which you can accidentally break.)

Finally there are classic snapshot formats like SNA, Z80, SNX ... those are by their nature destructive to the NextZXOS, and while for example I use SNA a lot in sjasmplus to quickly prototype/check something, I'm always doing that with the knowledge I'm taking full control over machine, and destroying OS variables is ok in such case.

-----------------------------------
Now with your specific TEST example - I would do this...
Your code as is is OK-ish toward ZX BASIC. You can save it as CODE block from address 32768 (0x8000) (it's 13 bytes long if I'm counting it correctly, but you can use the assembler symbols to not calculate it manually).

Then you need BASIC loader which will prepare the NextBASIC for such code block and start the code:

Code: Select all

10 CLEAR 32767 : REM moves stack under the 32768 where your code will be loaded
20 LOAD "your_code" CODE 32768 : REM load your code into memory
30 RANDOMIZE USR 32768 : REM and start it
Now you can pack both basic loader and the code block together into TAP file, and launch it on Next, it will return back to OS normally.

The PITA part is how to produce the BASIC loader if you are primarily interested into assembler and you are working for example in sjasmplus. That one has example TapLib in distribution for similar trivial loader, and EMPTYTAP/SAVETAP directives to build TAP file directly from asm source, but it's still not completely trivial. For your custom BASIC loader you either need to understand the binary form of tokenized BASIC and re-create it by using the DB/DW statements in assembly, or "cheat" by preparing the BASIC part on the Next itself, save it into TAP file, and then start every build of assembly by copying this loader-only TAP to final file as start, then add other blocks to it by SAVETAP...

But what is your reasoning to get back to BASIC in the first place, and can't you just soft-reset your app? (with CSpect itself it's even less clear what's your plan, I usually test my code and hit escape to close the CSpect completely, never been in need to get "back into the basic").
Thanks for good answers.

The reason to get back to basic is that I plan to do software that I can use in the NextOS that should get back to basic. At the moment I do like you said and repeat foreever and quit with escape.

I have to experiment a little more to see what is best here but probably I will be using .nex more tha being able to go back to basic again.
But ZXBasic is the language I started my career with so I don't want to 100% ignore that ;)

TheSwedishLord
Posts: 17
Joined: Mon Aug 17, 2020 7:47 am

Re: Why do I get this text instead of the text in upper left corner?

Postby TheSwedishLord » Sun Aug 30, 2020 10:27 pm

Tried to create a .tap file but it does not run, just return to basic with no output what so ever, not even with RANDOMIZE USR <ADDRESS_OF_ASM_CODE> : PAUSE 0
And if I peek at that address I see just NOP:s
Tried all SAVETAP from sjasmplus documentation. Any suggestions?

Ped7g
Posts: 256
Joined: Mon Jul 16, 2018 7:11 pm

Re: Why do I get this text instead of the text in upper left corner?

Postby Ped7g » Mon Aug 31, 2020 12:02 am

Post the full source, let me try it myself.
(I'm contributor to sjasmplus project, so I have difficult time to asses what is simple/difficult for regular users, as my perception is skewed heavily by knowing the actual implementation - so for me example from regular users are often eye-opening experience... :) )


Who is online

Users browsing this forum: No registered users and 2 guests