Xxx2bd Hex For

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

program xxx2bd

*
* Copyright 2022, Kevin G. Rhoads. All Rights Reserved.
* portions Copyright 2005, 1999, Kevin G. Rhoads. All Rights Reserved.
*
* Permission for use and distribution in accordance with
* GNU General Public License 2.0 or later is granted.
*
* Permission for use and distribution in accordance with
* Creative Commons CC-BY-SA License is granted.
*
* Source is F77 with MS/DVF extensions.
*
integer*1 abyte(16),ia
integer*2 aword(16),kk,nn,bytes,block,sector
character*1 a_char(16),a
equivalence (ia,a)
character*2 crlf
character*3 trichar(16)

character*127 arg,outfile
character drive*1,path*64,file*8,ext*3,sect*3
character base*8,orgbase*8,unitname*11,commonname*9
integer*4 nargs,argstat,offset,k,l
integer*2 curarg,i,j, i2and
logical*2 cmdline,endflag,dogoto
external nargs,getarg,fnovr

*---------

print *,' XXX2BD.FOR a program to generate BLOCK DATA sources '


print *,' from binary files '
print *,' Original version generates one 512 byte BLOCK DATA '
print *,' from the first 512 bytes of the file(s) on the command '
1 //'line'
print *,' '
print *,' Version 1.02 generates up to 1000 512 byte BLOCK DATA '
print *,' from the first bytes of the file(s) on the command '
1 //'line'
print *,' '
print *,' Version 1.03 May 29, 2022'
print *,' Version 1.02 February 1, 2022'
print *,' Version 1.01 January 31, 2022'
* print *,' Version 1.00 January 4, 2022'
print *,' '
print *,'Copyright 2022, Kevin G. Rhoads. All Rights Reserved. '
print *,' '
print *,'Permission for use and distribution in accordance with '
print *,'GNU General Public License 2.0 or later is granted. '
print *,' '
print *,'Permission for use and distribution in accordance with '
print *,'Creative Commons CC-BY-SA License is granted. '
print *,' '

curarg = 1
cmdline = (nargs().gt.1)

nn = 255
* change crlf to match system definition, what follows is right
* for MS-DOS
crlf = char(13)//char(10)

*-------------------------------MAIN LOOP over file names


9000 continue

if (cmdline) then
call getarg(curarg,arg,argstat)
* print *,' arg is ',arg
arg = arg(1:len_trim(arg))
* print *,' Trimmed arg is ',arg
curarg = curarg + 1
if ( curarg.gt.nargs() ) stop ' Bye - out of arguments'
else
stop ' Bye '
endif

*----

offset = 0
open (unit=10,file=arg,status='old',form='binary',err=9000)
call fnsep(arg,drive,path,base,ext)
drive = ' '
path = ' '
file = ' '
ext = 'FOR'
call fnovr(arg,outfile,drive,path,file,ext)
9013 continue
open (unit=11,file=outfile,status='unknown',form='formatted',
& err=9003)
dogoto = .false.
print *,'-------------------------------------------------------'
* print *,'Doing file ',arg(1:len_trim(arg))
print *,'Doing file ',arg
write (*,*) ' '
*
* Use following for DVF command line build initial alignment,
* that there are other problems with build via FL32 compatibility
* driver
* write (*,'(a1\)') ' '

ia = 0
a = base(1:1)
if (ia.ge.48.and.ia.le.57) then
base = 'a'//base
endif

orgbase = base
sector = 0

* write (11,7007) crlf


unitname = base(1:len_trim(base))//'_bd'
* write (11,7003) unitname,crlf
* write (11,7004) base,crlf
commonname = base(1:len_trim(base))//'c'
* write (11,7005) commonname,base,crlf
9023 continue

* write (11,7007) crlf


* unitname = base(1:len_trim(base))//'_bd'
* write (11,7003) unitname,crlf
* write (11,7004) base,crlf
* commonname = base(1:len_trim(base))//'c'
* write (11,7005) commonname,base,crlf
bytes = 0
blocks = 0
lines = 0

dogoto = .false.
if (sector.eq.0) print *,' starting '//unitname

*-------------------------------MINOR LOOP over bytes in file


9002 continue

* read up to 16 bytes, detect end of file


j = 0
endflag = .true.
do 1 i = 1,16
read (10,end=9001,err=9001) abyte(i)
kk = abyte(i)
aword(i) = i2and(kk,nn)
j = i
1 continue
endflag = j.eq.0
9001 continue

bytes = bytes + j
* if read returned bytes, process them
if (j.gt.0) then
do 2 i = 1,j
* not attempting to print control chars (ascii < 32)
*
* alternate "if" for designating over 126 as unprintable also
* if (abyte(i).ge.32.and.abyte(i).lt.127) then
* if (abyte(i).ge.0) then
if (abyte(i).ge.32.and.abyte(i).lt.255) then
a_char(i) = char(abyte(i))
else
* using ~ for unprintables
a_char(i) = '~'
endif
* use z2.2 for DVF and z2 for MS Fortran 5.1 if you need HEX
* write (trichar(i),'(i4.3)') aword(i)
write (trichar(i),'(i3.3)') aword(i)
2 continue

* blank out unused on last partial line, if such


do 3 i = j+1,16
a_char(i) = ' '
trichar(i) = ' '
3 continue
*
* write to DAT, set crlf to line terminator of your preference
block = (bytes / 64) + 1
lines = lines + 1
if (lines.gt.4) lines = 1

if (lines.eq.1) then
if (block.eq.1) then
*
* start new block data, new common &c
write (11,7007) crlf
unitname = base(1:len_trim(base))//'_bd'
write (11,7003) unitname,crlf
write (11,7004) base,crlf
commonname = base(1:len_trim(base))//'c'
write (11,7005) commonname,base,crlf
*
* write block 1, line 1
write (11,7031) base,crlf
dogoto = .false.
elseif (block.eq.2) then
write (11,7032) base,crlf
elseif (block.eq.3) then
write (11,7033) base,crlf
elseif (block.eq.4) then
write (11,7034) base,crlf
elseif (block.eq.5) then
write (11,7035) base,crlf
elseif (block.eq.6) then
write (11,7036) base,crlf
elseif (block.eq.7) then
write (11,7037) base,crlf
elseif (block.eq.8) then
write (11,7038) base,crlf
dogoto = .true.
endif
write (11,7001) (trichar(i),i=1,16),(a_char(i),i=1,16),
& crlf
elseif (lines.eq.2) then
write (11,7011) (trichar(i),i=1,16),(a_char(i),i=1,16),
& crlf
elseif (lines.eq.3) then
write (11,7012) (trichar(i),i=1,16),(a_char(i),i=1,16),
& crlf
elseif (lines.eq.4) then
write (11,7021) (trichar(i),i=1,16),(a_char(i),i=1,16),
& crlf
if (dogoto) then
bytes = 0
sector = sector + 1
*
* 160k diskettes have 320 sectors
* 320k diskettes have 640 sectors
* this version of XXX2BD can handle up to 999 additional blocks
if (sector.gt.4095.or.endflag) then
goto 9003
endif
write (sect,'(z3)') sector
base = orgbase(1:(len_trim(orgbase)-3)) // sect
dogoto = .false.
offset = offset + j
k = offset/512
l = offset - k*512
if (l.eq.0) then
write (11,7002) crlf
write (11,7006) crlf
* goto 9003
endif
if (.not. endflag) goto 9023
endif
endif
else
* no bytes read
goto 9003
endif

offset = offset + j
k = offset/512
l = offset - k*512
if (l.eq.0) then
write (11,7002) crlf
write (11,7006) crlf
* goto 9003
endif
if (.not. endflag) goto 9002

*----
9003 continue
print *,' Ended with '//unitname
close (unit=11,status='keep',err=9004)
9004 continue
close (unit=10,status='keep',err=9005)
9005 continue

if ( curarg.lt.nargs() ) goto 9000

stop ' Bye'

*with blank
7001 format (5x,'1/',15(a3,','),a3,', |',8a1,' ',8a1,a2\)
7011 format (5x,'2 ',15(a3,','),a3,', |',8a1,' ',8a1,a2\)
7012 format (5x,'3 ',15(a3,','),a3,', |',8a1,' ',8a1,a2\)
7021 format (5x,'4 ',15(a3,','),a3,'/ |',8a1,' ',8a1,a2\)
7002 format (1x,a2\)
7003 format (6x,'block data ',a11,a2\)
7004 format (6x,'integer*1 ',a8,'(512)',a2\)
7005 format (6x,'common /',a9,'/',a8,a2\)
7006 format (6x,'end',a2\)
7007 format ('*','-------------------------------------------------',
1 '---------------------',a2\)
7031 format (6x,'data (',a8,'(i),i=1,64)',a2\)
7032 format (6x,'data (',a8,'(i),i=65,128)',a2\)
7033 format (6x,'data (',a8,'(i),i=129,192)',a2\)
7034 format (6x,'data (',a8,'(i),i=193,256)',a2\)
7035 format (6x,'data (',a8,'(i),i=257,320)',a2\)
7036 format (6x,'data (',a8,'(i),i=321,384)',a2\)
7037 format (6x,'data (',a8,'(i),i=385,448)',a2\)
7038 format (6x,'data (',a8,'(i),i=449,512)',a2\)

end
*------------------------------------------------------
integer*2 function i2and(i,j)
integer*2 i,j
i2and = i .and. j
return
end

You might also like