Write binary data to TRD from ASM

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
keith56
Berk
Posts: 7
Joined: Mon Nov 13, 2017 9:23 pm

Write binary data to TRD from ASM

Post by keith56 » Thu Dec 07, 2017 11:19 pm

I'm trying to write a file to a TRD image using assembly, and I'm having some trouble.
I was wondering if anyone had a small code sample of how to do it so I can use that as a template,
as I'm clearly misunderstanding something, as I can't see what I'm doing wrong!

I need to write about 60 bytes of data from ram to a named file (not sector) on disk, it's to save settings and highscore.

Any help will probably save me a lot of time
Thanks,
Keith
1 x
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter for CPC - http://www.chibiakumas.com
「チビ悪魔」可笑しいゴシックSTG: http://www.chibiakuma.com
Chibi Akumas Episode 2:Confrontation for CPC ...Out Now!

User avatar
Seven.FFF
Berk
Posts: 39
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: Write binary data to TRD from ASM

Post by Seven.FFF » Fri Dec 08, 2017 4:20 am

I'm also interested in this. Coincidentally I was reading around this earlier, and came across the following two snippets. Any use? They seem to describe two different ways of making TR-DOS calls.

Code: Select all

                ÖÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ·
                º TR-Dos system functions. º
                ÓÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĽ

The  system  functions  work  with file and logical level of
disk structure. The entry point for system functions call is
15635.  If  TR-Dos  is  not  initialized,  init.  subroutine
(15649)  must be called before system calls. Function number
is placed in C register.

ÖÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ·
º C   ³Function                                              º
ÇÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĶ
º Start of work with disk                                    º
º #00 ³Microcontroller reset                                 º
º #01 ³Read drive parameters. A=drive No. (0-3)              º
º #18 ³Read  disk parameters. 9th sector is read into buffer º
º     ³placed after TR-Dos variables                         º
º Logical level                                              º
º #05 ³Read sectors. HL=memory address, DE=start sector/trackº
º     ³B=number of sectors                                   º
º #06 ³Write sectors. Parameters are the same                º
º File level                                                 º
º #07 ³Write current disk catalog into stream (number in A)  º
º #08 ³Read catalogue entry (A=No.) into descriptor          º
º #09 ³Write catalogue entry from descriptor                 º
º #0A ³Find entry, placed in descriptor, in disk catalog.    º
º     ³#23814-number  of  bytes  to  match.  Returns:C=entry º
º     ³found, #FF, if no                                     º
º #0B ³Create  file.  HL=start  address, DE=length, name and º
º     ³type are in descriptor.                               º
º #0C ³Create BASIC-program (detailed previous function)     º
º #0E ³Read/verify  file.  #23801:#00-read, #FF-verify. Also º
º     ³can  be given: HL-start, DE-length. If A=0, start and º
º     ³length  are  taken from catalog, A=3 - only length is º
º     ³taken  from catalog, other cases - both HL and DE are º
º     ³significant.                                          º
º #12 ³Erase file. Name, type etc. are in descriptor.        º
º Service functions                                          º
º #13 ³Copy 16b from HL to descriptor                        º
º #14 ³Copy 16b from descriptor to HL                        º
º #17 ³Choose bottom disk side                               º
º #15 ³Test  physical track while formatting. Track is given º
º     ³in  D, side - with previous operation. 23766 - number º
º     ³of bad sectors on track                               º
ÓÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĽ
Other  functions  are  used  by  TR-Dos  and are useless for
programmer.

Other    themes:    TR-Dos    system   variables,   BetaDisk
microcontroller programming.
                 Igor Yudincev (igor@karlson.kai.ksu.ras.ru)
                 Text is written in IS-Editor v.1.1, 1.06.97

Code: Select all

15   MACHINE CODE PROGRAMMING

The inclusion of TRDOS  routines  in  machine  code  programs  are
relativey straightforward.

There are 3 elements required for the program:

(1) The machine code equivalent of the BASIC TRDOS command.
(2) The machine code routine to implement 1 above.
(3) The machine code routine to restore the system to its original
    state before calling and executing the command.

The actual memory locations used will depend upon the program as a
whole. For the  purpose  of  this  example  the  location  of  the
routines  will be at 49000 for the SAVE, at 49500 for the LOAD and
at 50000 for the instructions calling them. Thus item 1 will be at
either 49000 or 49500 and items 2 and 3 at 50000.

EXAMPLE:

Address     Code     Basic   Comment

49000       234      REM     Codes as in Appx A of Spectrum
49001        58       :      manual
49002       248      SAVE
49003        34       "
49004        69       E
49005       120       x
49006        97       a
49007       109       m      File name "Example"
49008       112       p
49009       108       l
49010       101       e
49011        34       "
49012        13      ENTER   Always end with ENTER

The  code for the LOAD commences at  49500 and is eaxctly the same
as above except that address 49502 will contain 239 (LOAD) instead
of 248 (SAVE).

These two  routines, LOAD, and  SAVE can be  located anywhere, but
the  initiator which we are locating at 50000 requires changing at
addresses  50007-50008 (Save  routine address)  and 50025  - 50026
(LOAD routine address) to point to the new addresses.

To relocate, the  initiator itself  requires reassembly.  For that
reason the Z80 mnemonics only are given below.

CHADD  EQU   23645         Location of SOS variable CHADD
       ORG   XXXXX         XXXX=address of this code
       LD    HL,(CHADD)    Start to save true CHADD
       LD    (TEMP), HL    Temporary store of true CHADD
       LD    HL,49000      Address of SAVE routine
       LD    (CHADD),HL    CHADD now points to our routine
       CALL  15363         ENTER TRDOS SAVE via chadd
       JP    BACK          Jump to program point from which
                           the whole routine was called
       LD    HL,(CHADD)    
       LD    (TEMP),HL     The routine for LOAD now repeats
       LD    HL,49500      the above with just the address
       LD    (CHADD),HL    changed.
       CALL  15363         
BACK   LD    HL,(TEMP)     Start to restore CHADD
       LD    (CHADD),HL    Reload original CHADD
       RET                 Return from where you came
TEMP                       Label allocating memory for
                           temporary storage

The whole routine  pointing to  both the  SAVE and  LOAD routines,
together with the  "return  to  point  of  entry"  ending  routine
occupies only 47 bytes.
0 x
Robin Verhagen-Guest
colonel32
seven-fff.com
threetwosevensixseven.com

User avatar
keith56
Berk
Posts: 7
Joined: Mon Nov 13, 2017 9:23 pm

Re: Write binary data to TRD from ASM

Post by keith56 » Fri Dec 08, 2017 9:54 am

I'm using the "C = # 0B - write to the disk CODE file type" - or at least trying to, as TR-Dos is getting stuck in an infinate loop.
when I was trying to figure out writing Hikaru gave me a great source sample, but even that didn't work, I had to hack my destination address into the Tr-Dos variables to get it to load to the destination I specified:
https://www.worldofspectrum.org/forums/ ... rt-planned

Here's the final code I'm using to load - and it works fine so far:

Code: Select all

DiskDriver_Load:
ifdef buildZXS_SPUNSAFE
	ld (SPRestore_Plus2-2),sp
	ld sp,StackPointerAlt
endif
	push de
		push hl
			ld iy,&0000

			di
			LD C,0		;DOS_RESET
			CALL &3D13 

			LD A,0		;Disk drive initialisation
			LD C,1
			CALL &3D13 

			LD C,&18
			CALL &3D13 

		pop hl

;			LD HL,filename ; to create a descriptor  
  			LD C,&13  
  			CALL &3D13  
    
  			LD C,&0A; find file  
  			CALL &3D13  

 			LD A,C  
  			INC C  
  			JR Z,  DiskDriverLoad_Error  

  			LD C,&08  ;Read file descriptor to DOS  variable  area
  			CALL &3D13  

  			XOR A  
  			LD (&5CF9),A  
  			LD (&5D10),A  
	pop hl

;  			LD HL,&9000 ;Destination  
			ld (&5CE6),hl
			ld (&5CED),hl
			LD A,&FF  

  			LD C,&0E ; read from disk into memory area for an address  
  			CALL &3D13  
			
			DiskDriverLoad_Done:
ifdef buildZXS_SPUNSAFE
			ld sp,&0000 :SPRestore_Plus2
endif
			ret
I'm building my trd files with trdtool, which is great, butI'm wondering if the files its making are not quite right, as I seem to be having a lot of trouble.

https://shiru.untergrund.net/software.shtml

But then again I had a lot of trouble in the early days becaue TRDos changed their specification between ver 4 and 5, and commands all changed, with "RANDOMIZE USR 15360" changed to "RANDOMIZE USR 15616" causing many of the samples of source-code I found could not work... not that this can be a problem here, I can't find a single example of a TRD write command online so far.

A lot of the documentation is in russian, which leaves me wondering if the problems are translation ones... Hikaru's example says 'ld (#5CF9),a ;#00: load, #FF: save'.... but other documentation says 'Write #00 to #5CF9 if you want Load or #FF if you want Verify file'...
it makes working things out very hard, so it would be a help if anyone had a code fragment they knew worked!
0 x
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter for CPC - http://www.chibiakumas.com
「チビ悪魔」可笑しいゴシックSTG: http://www.chibiakuma.com
Chibi Akumas Episode 2:Confrontation for CPC ...Out Now!

Ralf
Microbot
Posts: 131
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Write binary data to TRD from ASM

Post by Ralf » Fri Dec 08, 2017 12:55 pm

Maybe try posting to zx.pk.ru I suppose it's the best place to get the answer. It's Russian forum but they have an English section if I remember correctly.
0 x

Hikaru
Dizzy
Posts: 51
Joined: Mon Nov 13, 2017 1:42 pm
Location: Russia
Contact:

Re: Write binary data to TRD from ASM

Post by Hikaru » Fri Dec 08, 2017 1:34 pm

My bad. Overall I've adapted that example from the book, 'ZX-Spectrum & TR-DOS Для пользователей и программистов' page 194. However, it looks like I've made a number of mistakes while doing that:

1. The '#00: load, #FF: save' comment is indeed wrong and should read '#FF: verify' instead. Saving is done with another function (C=#0B).

2. The input value in A is decoded as:
A = 0: loads the file using the default loading address and length defined at save time.
A = 3: assumes HL = loading address, and DE = length.
A = Any other value: assumes HL = loading address and uses the default length defined at save time.

3. There's actually a point to the CALL: RET construction at the end because otherwise this routine might not work from BASIC (the TR-DOS ROM would still be switched on at the time of return).

The corrected example for loading a file in TR-DOS to a custom address should therefore read as follows:

Code: Select all

LoadExample
	ld hl,FileDescriptor
	ld c,#13		;#3D13 C=#13: copy the file descriptor to TR-DOS system variables
	call #3D13
	xor a
	ld (#5CF9),a		;#00: load, #FF: verify
	dec a			;load the file to an address in HL
	ld hl,MyAddress
	ld c,#0E
	call #3D13		;#3D13 C=#0E: load/save file
	ret

FileDescriptor
	DEFB "FILENAME"		;8 characters
	DEFB "C"		;file type: code

Now for saving files. This will check if a file with a given name exists, delete it if it does, and save the file. Notice that the file isn't physically deleted but only 'marked as' deleted, which is the same as doing an ERASE in TR-DOS prompt. In case it was not the last file on the disk, it might be necessary to issue a MOVE in TR-DOS prompt in order to make sure this disk space isn't 'lost touch with'.

Code: Select all

SaveExample
	ld hl,FileDescriptor
	ld c,#13		;copy the file descriptor to TR-DOS system variables
	call #3D13
	ld a,9			;find a file with matching 9 first bytes in the descriptor, i.e. just the filename and type
	ld (#5D06),a
	ld c,#0A
	call #3D13
	inc c			;C will contain #FF if the file with this name wasn't found
	ld c,#12		;if it was found, mark the file descriptor as deleted
	call nz,#3D13
	ld hl,Address		;save the file
	ld de,Length
	ld c,#0B
	call #3D13
	ret

FileDescriptor
	DEFB "FILENAME"		;8 characters
	DEFB "C"		;file type: code
One more thing: it is typically advised to set IY to #5C3A before calling any sort of system/ROM functions.

Hope this helps.
1 x

User avatar
Seven.FFF
Berk
Posts: 39
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: Write binary data to TRD from ASM

Post by Seven.FFF » Fri Dec 08, 2017 2:51 pm

keith56 wrote:
Fri Dec 08, 2017 9:54 am
TRDos changed their specification between ver 4 and 5, and commands all changed, with "RANDOMIZE USR 15360" changed to "RANDOMIZE USR 15616"
How do you write code that works on all versions? Or do you just assume that everyone is running v5 now?
0 x
Robin Verhagen-Guest
colonel32
seven-fff.com
threetwosevensixseven.com

User avatar
Seven.FFF
Berk
Posts: 39
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: Write binary data to TRD from ASM

Post by Seven.FFF » Fri Dec 08, 2017 3:32 pm

keith56 wrote:
Fri Dec 08, 2017 9:54 am
I'm building my trd files with trdtool, which is great, butI'm wondering if the files its making are not quite right, as I seem to be having a lot of trouble.

https://shiru.untergrund.net/software.shtml
I found this too, and it's working nicely. Shiru's stuff is great.

How are you creating your Basic boot file? trdtool takes a tokenized file not an ascii file. This stumped me for a while, then I used bas2tap to tokenize it, and tzx_display to extract the headerless program block from the tap.

Is there a more straightforward way to do this?
0 x
Robin Verhagen-Guest
colonel32
seven-fff.com
threetwosevensixseven.com

Hikaru
Dizzy
Posts: 51
Joined: Mon Nov 13, 2017 1:42 pm
Location: Russia
Contact:

Re: Write binary data to TRD from ASM

Post by Hikaru » Fri Dec 08, 2017 5:45 pm

keith56 wrote:
Fri Dec 08, 2017 9:54 am
But then again I had a lot of trouble in the early days becaue TRDos changed their specification between ver 4 and 5, and commands all changed, with "RANDOMIZE USR 15360" changed to "RANDOMIZE USR 15616" causing many of the samples of source-code I found could not work..
I believe it is entirely safe to ignore any versions below 5.03. v5.01 has seen some usage in the 90's around here in Russia/CIS but was quickly replaced by v5.03. If you're keeping to documented entry points such as #3D13 then they are compatible and should behave identically.
1 x

User avatar
keith56
Berk
Posts: 7
Joined: Mon Nov 13, 2017 9:23 pm

Re: Write binary data to TRD from ASM

Post by keith56 » Sat Dec 09, 2017 12:32 am

YES! it's working great, thanks Hikaru!

it seems my problem was not the code, or trdtool, but something with my emulator, I was using TR-DOS 5.04 (UK) and it was somehow failing to even save a bas file to a newly formatted disk from it's own interface (It was renaming the disk??!!)

Well, I've moved to 6.04 (Russian) and the example works perfectly, so I don't know if I had a corrupt rom, or something messed up on my emulator, but all is good now! - out of interest, what is the best rom to use for testing (compatibility wise?)

at the moment I'm starting my game from a memory snapshot, not a bas file on the disk - it's possible that was part of my problem, but the problem seemed to persist after a cold reboot with saving the bas in tr-dos.

Thanks again! great to know there's so many people who know their stuff, and are willing to help!
Keith

Oh, Ralf on WOS gave me some russian PDF's and I managed to translate one using google - the result is quite useful, I've linked it below in case it helps anyone:
http://www.chibiakumas.com/download/TR- ... ateurs.pdf
0 x
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter for CPC - http://www.chibiakumas.com
「チビ悪魔」可笑しいゴシックSTG: http://www.chibiakuma.com
Chibi Akumas Episode 2:Confrontation for CPC ...Out Now!

Hikaru
Dizzy
Posts: 51
Joined: Mon Nov 13, 2017 1:42 pm
Location: Russia
Contact:

Re: Write binary data to TRD from ASM

Post by Hikaru » Sat Dec 09, 2017 8:33 am

keith56 wrote:
Sat Dec 09, 2017 12:32 am
out of interest, what is the best rom to use for testing (compatibility wise?)
Well, the original 5.03 is considered the baseline version of sorts. This is the last known official release. Another widespread edition is the unofficial 5.04T, which comes as the default ROM in some major 'Pentagon'-oriented emulators, most notably UnrealSpeccy. As far as I know, this is mostly unchanged 5.03 with several bugs fixed by enthusiasts. It's probably a good idea to target either one of those.
There were subsequent unofficial bugfix editions and mods, but they are less known.

I've uploaded a commented disassembly of 5.04T by Pavel Fedin and Spectre here. I find this useful to refer to when I don't understand what's going wrong. The commentary is in Russian, but I believe googletranslate should do a reasonable job with it.
0 x

Post Reply