News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

reading a binary file into memory using basic

Started by arnoldemu, 09:24, 27 February 16

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

arnoldemu

I need help  :o

Is it possible to read all the bytes of a file using BASIC?
I tried to use LINE INPUT but that just gets the text, bytes like 0 etc are ignored.
I tried using INPUT#9,a% and that didn't seem to work either. I saw a lot of 0's and not the decimal values I expected.
Is there a way in pure BASIC to do it?

I can write some binary code and poke it in, but i wondered if there was a way to do it from BASIC.

The reason I ask is that I want to load a spectrum+3 basic program into memory, and CPC doesn't see it as binary because it has it's own header.

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

FloppySoftware

#1
Try:

LOAD filename, address

If that don't work: sorry, but mi computer is a PCW, not a CPC!

BTW, OPENIN seems to work only for ascii / text files, not binary ones.
floppysoftware.es < NEW URL!!!
cpm-connections.blogspot.com.es

ronaldo

Yes, it is posible: exactly as @FloppySoftware says:
LOAD filename$, address


However, you have to be cautious. You cannot write into BASIC memory space. You have to free that space previously. A normal way to do it would be as follows:

MEMORY &7FFF: REM Free memory from &8000 onwards
LOAD "myfile.bin", &8000: REM Load a binary file directly into &8000



andycadley

Quote from: ronaldo on 10:29, 27 February 16
Yes, it is posible: exactly as @FloppySoftware says:
LOAD filename$, address


However, you have to be cautious. You cannot write into BASIC memory space. You have to free that space previously. A normal way to do it would be as follows:

MEMORY &7FFF: REM Free memory from &8000 onwards
LOAD "myfile.bin", &8000: REM Load a binary file directly into &8000


I'm pretty sure that LOAD filename, address won't work on a file that lacks an AMSDOS header. I think you'll have to go to assembly to load a headerless file like that.

AMSDOS

I'm not sure if that's what @arnoldemu is asking for though. I think he's after the Bytes of the Header as well as the Program? A Header Reader would return that information, but I've only ever done it for CPC files, I don't know the structure of a spectrum +3 BASIC file.
* Using the old Amstrad Languages :D * And create my own ;)
* Incorporating the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

arnoldemu

Quote from: FloppySoftware on 10:22, 27 February 16
Try:

LOAD filename, address

If that don't work: sorry, but mi computer is a PCW, not a CPC!

BTW, OPENIN seems to work only for ascii / text files, not binary ones.
Sorry doesn't work. It was the first thing I tried.

LOAD filename,address gives an error.

I then tried:
LOAD filename

which actually works!? And the plus3 basic program is loaded around 6c00. But then it blanks any basic program.

So I tried openin,input#9,closein. But none of them read byte by byte.

So looks like I will need to write some asm using cas in char etc.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

ronaldo

Sorry, @arnoldemu , I didn't fully understand what you were asking for. That's my fault for not thoroughly reading your post.


OPENIN, OPENOUT and company have their own "ASCII-way" of storing and reading things. They are not useful for reading any binary data. I tried it some time ago. If you look at the way they store data, it is completely clear why you can't do any magic for reading binaries:
[attach=2]


They insert spaces to separate ASCII values and they also encode each ASCII value into 1-to-3 bytes with the ASCII characters that form the digits of the number. It's quite nonsensical. So, it seems clear that you have to do your own low-level reader to read binary data without an AMSDOS header.

FloppySoftware

Quote from: arnoldemu on 10:50, 27 February 16
Sorry doesn't work. It was the first thing I tried.

LOAD filename,address gives an error.

I then tried:
LOAD filename

which actually works!? And the plus3 basic program is loaded around 6c00. But then it blanks any basic program.

So I tried openin,input#9,closein. But none of them read byte by byte.

So looks like I will need to write some asm using cas in char etc.

Oops! I told you I'm a PCW user!

Anyway, maybe you could just add an amsdos header to the binary file in order to be able to load it.
floppysoftware.es < NEW URL!!!
cpm-connections.blogspot.com.es

arnoldemu

I stepped through basic and tracked the cas in char.

It is as ronaldo says, it works with text only. It reads a byte and tests if it is a letter or number and from that it reads and tries to read it as text

Using line input almost worked. Some of the bytes were transfered into the string. So it may be possible this way and to split the char into bytes and poke the into memory but it reads up to the end of line character. I don't think the end of line characters are in the string and I don't know what happens with '0' chars.

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Executioner

Quote from: arnoldemu on 09:24, 27 February 16
Is there a way in pure BASIC to do it?

Why do you need to do it in pure BASIC? You could use a M/C routine to read it, you can read as ASCII using CAS_IN_CHAR. Keep reading if you get a "soft" EOF (ASCII 26).

arnoldemu

I have a BASIC program that reads a Spectrum BASIC file and lists it. So if I could do it all with basic it would have been nicer. I'll do the binary and call it.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

HAL6128

#11
How is the source code: binary, hexadezimal numbers?

The openin command should work if the file hasn't a header. Or change the header Information to an ascii file?!

...convert every single reading character with the VAL function for numbers and ASC function minus 55 with characters (if it's hexadezimal).
Did I understand you right?

SRS

You know the file format ? If it is tokenized, maybe this can help: Sinclair BASIC tokenized file - Just Solve the File Format Problem. Else it should be pure Ascii.

arnoldemu

Thank you for all the help. I have solved it with some data statements and poking the machine code into memory.

I wish CPC had BPUT or BGET like BBC BASIC, but it doesn't.

The file I am loading doesn't have an AMSDOS header so CPC will think it is ASCII, but it's binary and it's tokenised Spectrum BASIC program.

I wanted to load the file without re-saving it because I want to be able to insert a spectrum+3 disc directly and run the program to list it without modifying any files.

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Powered by SMFPacks Menu Editor Mod