Page 1 of 3

Converting .BIN files to .PRG (Split/moved from HW/Tech)

Posted: Thu Jul 20, 2006 1:59 am
by Boray
I suspect all files even sequencial files have those two bytes. So I think it would be best to go outside of the vic (or Vice) to make a converter. A program on the PC for example...

Posted: Thu Jul 20, 2006 2:50 am
by eslapion
Boray wrote:I suspect all files even sequencial files have those two bytes. So I think it would be best to go outside of the vic (or Vice) to make a converter. A program on the PC for example...
Nope, a sequential file does not have an address header. It is a sequence... of data.

Programming, at least to me, is a lot more complicated on the PC than it is on a VIC or 64...

Posted: Thu Jul 20, 2006 3:04 am
by Boray
You are right of course... How else would all my text converting programs work! ;)

Posted: Thu Jul 20, 2006 5:40 am
by eslapion
Boray wrote:You are right of course... How else would all my text converting programs work! ;)
If you programmed text converting software then you must be able to tell me how to do the conversion I want to do with .bin to .prg and .prg to .bin, right?

Posted: Thu Jul 20, 2006 6:16 am
by Boray
I thought you wanted to do it yourself?

The problem is that it's very slow to read byte by byte (with GET#) so the program will get very slow...

something like this maybe:

(bin to prg)

10 open 1,8,0,"input"
20 open 2,8,1,"output"
30 print #2,chr$(b1);chr$(b2); :rem address bytes

50 get#1,a$
60 if st<>0 then close1:close2:end
70 print#2,a$;
80 goto 50

Posted: Thu Jul 20, 2006 6:20 am
by Boray
It speeds up quite a bit by reading more than one byte at a time:

get #1,a$,b$,c$,d$

print #2,a$;b$;c$;d$;

But the trouble is then that the end of the file won't be exact. You will most likely get some extra bytes, but I have used this in my converting programs anyway...

Posted: Thu Jul 20, 2006 6:42 am
by Boray
Maybe some moderator should move this topic to the programming forum?

Posted: Thu Jul 20, 2006 8:55 am
by eslapion
Boray wrote:The trouble is then that the end of the file won't be exact. You will most likely get some extra bytes, but I have used this in my converting programs anyway...
.BIN files for rom images are always a multiple of 1024 so, unless I use more than 1024 bytes transfered at a time, there shouldn't be any problems.

In your program, there is an "output" opened channel but I expected this only to write the .bin to memory at an address of my choosing.

Doing the file the way you tell me seems to work as it created a new file with 2 extra bytes at the beginning, however the result is also a sequential file and not a prg.

I think what I want, judging from what you gave me here, should be more like:

5 rem acquisition routine by Boray
10 open 1,8,0,"input"
15 t=0
20 get#1,a%
30 poke8192+t,a%
40 t=t+1
50 if st<>0 then close 1:goto70
60 goto20
65 rem save routime given by Mike
70 SYS57809"myprgfile",8,1:POKE193,0:POKE194,32
80 POKE780,193:POKE781,0:POKE782,(8192+(t+1)/256):SYS65496

Assuming I want to have 8192 as a starting address.

If there is any major errors here, and particularly on lines 20-30, by all means, please tell me.

Posted: Thu Jul 20, 2006 8:58 am
by carlsson
I am an Emacs person. Unless the source or destination file has to be on a Commodore disk image, I would use Emacs to add or remove two bytes at the beginning of a file. Other editors might work as well, but not all are binary safe.

I think some PC side CBM tool has built-in functions for doing this, but I'm not sure which.

Posted: Thu Jul 20, 2006 9:00 am
by carlsson
eslapion wrote: 20 get#1,a%
30 poke8192+t,a%
You can only GET# (and GET) string variables, so it should read:

20 GET#1,A$:IFA$=""THENA$=CHR$(0)
30 POKE8192+T,ASC(A$)

The IF statement is because empty string will cause error with ASC.

Posted: Thu Jul 20, 2006 10:56 am
by Mike
eslapion wrote:I think what I want, judging from what you gave me here, should be more like:

5 rem acquisition routine by Boray [...]
An even better version is:

Code: Select all

10 DEFFNHI(X)=INT(X/256)
20 DEFFNLO(X)=X-256*FNHI(X)
30 OPEN2,8,2,"input,p,r":S=8192:T=S
40 GET#2,A$:IFST=0THENPOKET,ASC(A$+CHR$(0)):T=T+1:GOTO40
50 CLOSE2:SYS57809"myprgfile",8,1:POKE193,FNLO(S):POKE194,FNHI(S)
60 POKE780,193:POKE781,FNLO(T):POKE782,FNHI(T):SYS65496
Note: ASC operates only on the first character of its argument.

Michael

Posted: Thu Jul 20, 2006 11:58 am
by eslapion
Hey! Mike to the rescue again!

Thanks!

Now what's missing is how to go the other way around...

Posted: Thu Jul 20, 2006 12:17 pm
by Jeff-20
Boray wrote:Maybe some moderator should move this topic to the programming forum?
Are you not a mod in this forum? :lol: :wink:

Posted: Thu Jul 20, 2006 1:26 pm
by tlr
If you are on a unix-like system you could do this...

bin2prg:

Code: Select all

echo -e -n '\x00\x40' | cat - file.bin > file.prg
(load addr $4000 in the example)

prg2bin:

Code: Select all

dd if=file.prg of=file.bin bs=1 skip=2
(any load addr)

Posted: Thu Jul 20, 2006 3:54 pm
by eslapion
tlr wrote:If you are on a unix-like system you could do this...

bin2prg:

Code: Select all

echo -e -n '\x00\x40' | cat - file.bin > file.prg
(load addr $4000 in the example)

prg2bin:

Code: Select all

dd if=file.prg of=file.bin bs=1 skip=2
(any load addr)
Sorry, I am a slave to Bill... I'm a Windows guy and I don't even know how to make programs under that OS...