;+
; NAME:
;	BCS_CONV
; PURPOSE:
;	Converts Intel-hex ouput of cross-assembler to various loads
; CATEGORY:
; CALLING SEQUENCE:
;	.run bcs_conv
; INPUTS:
;	used specified .hex files
; OPTIONAL INPUT PARAMETERS:
; KEYWORD PARAMETERS:
; OUTPUTS:
;	creates ".hgs" (NEC patch format) and ".hcx" (dc and bc commands) 
;	files on current directory
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
; PROCEDURE:
; MODIFICATION HISTORY:
;	RDB  Autumn-90	Written, modelled on ROSAT WFC code
;	RDB   5-May-94	"PROG WRITE" only on new address segment
;			made hex chars lower case in .hcx file
;	RDB  17-May-94	Changed "BCS-" to "07-", the BCS instrument code
;			Documented.
;-

iadd=0L
iadd_old = iadd
ii=intarr(16)

filename=''
read,'* Enter Filename: ',filename
longf = ''
read,'* Put PROG WRITE/BC EXEC around WRITE BC?: ',longf
longf = strupcase(strmid(longf,0,1))

filename = strupcase(filename)
wheredot = strpos(filename,'.')
len = strlen(filename) & if wheredot gt 0 then len = wheredot
filename = strmid(filename,0,wheredot)

openr,1,filename+'.hex'		;/point'
openw,2,filename+'.hcx'
openw,3,filename+'.hgs'
ich = byte(0)
nbytes = 0
nbytes_old = nbytes
ss=''

;;printf,3,'SC=SOLAR-A'
;;printf,3,'SS=BCS'
;;printf,3,'OBC=BCS'
;;printf,3,'SRC=BCS/MSSL/RDB'
;;systime = !stime
;;printf,3,'DATE=',strmid(systime,0,11)
;;printf,3,'*'
;;printf,3,'*   patches to BCS CDHS micro load 6D/05'
;;printf,3,'*     (post-launch file ',filename,')'
;;printf,3,'*'

;>>	prepare file requied by NEC

write_nec:
aaa= strupcase(!stime)
yr = strmid(aaa,7,4)
dy = string(fix(strmid(aaa,0,2)),'(i2.2)')
months = 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'
month = strmid(aaa,3,3)
im = strpos(months,month)/3+1 ;& print,im
mn = string(im,'(i2.2)') ;& print,mn
date = yr + '/' + mn +'/' + dy
print,date

nrecords = 0

nrec = string(nrecords,'(Z4.4)')
version = ''		;0
read,'Version number (max of 4 chars): ',version
ver = strupcase(strmid(version,0,4))		;ver = string(version,'(Z4.4)')
header = 'SOLAR-A     BCS         ' + nrec + ver + ' BCS PATCH DATA ' + date
print,header

;;ss=''
;;openw,3,'bcs' + ver + '.hex'	;strmid(ver,2,2) 
;	header record
printf,3,header

star='***********************************************************************'
PRINTF,2,star
printf,2,'; Filename: ', filename + '.hcx'
printf,2,'; Converted from file: ' + filename + '.hex  on ' + systime()
PRINTF,2,star

while not eof(1) do begin
;;	readf,1,ss
;;	if strmid(ss,0,1) eq '*' then begin		;comment
;;	  print,ss
;;	  goto, next
;;	  endif
;		if not comment, re-read as data, and see how many bytes
;;	point_lun,-1,j & point_lun,1,j
	point_lun,-1,j
	readf,1,format='(x,Z2,Z4)',nbytes,iadd
;		re-read again, for the data, depending on number of bytes
	point_lun,1,j
	if nbytes eq 0 then begin
	  readf,1,ss
	  print,'End found'
;;	  printf,3,ss
	  goto,endit
	  endif
	ii=intarr(nbytes)
	readf,1,format='(9x,16Z2)',ii

	print,format='(x,Z2.2,2x,Z4.4,4x,16Z3.2)',nbytes,iadd,ii
;;	printf,3,ss
;	  write the NEC record
	  addr = string(iadd,'(Z4.4)')
	  nbyt = string(nbytes,'(Z2.2)')
	  data = string(ii(0:nbytes-1),'(16Z2.2)')
	  sso='00' + '00' + addr + nbyt + data
	  print,sso
	  printf,3,sso

	if iadd ne iadd_old+nbytes_old then begin	;new address field
	  badd = byte(iadd,0,2)
	  PRINTF,2,' '
	  printf,2,format='(a,t25,a)','07-DC-19','PROG AD SET'
	  printf,2,format='(a,z2.2,t27,1h(,z2.2,1h,,z2.2,1h))' $
		,'07-BC-',badd(1),badd(1),badd(0)
	  printf,2,format='(a,z2.2)', '07-BC-',badd(0)
	  if longf eq 'Y' then printf,2,format='(a,t25,a)','07-DC-11','BC EXEC'

	  PRINTF,2,' '
	  if longf ne 'Y' then $
		printf,2,format='(a,t25,a)','07-DC-1A','PROG WRITE'
	  endif

	for jj=0,nbytes-1,2 do begin
;;	  printf,2,format='(a,t25,a)','BCS-BC-12','BC ENA'
;;;	  if jj eq 0 then $
;;;		printf,2,format='(a,t25,a)','BCS-DC-1A','PROG WRITE'
          if jj eq 0 then printf,2,' '
	  if jj gt 0 and longf eq 'Y' then $
		printf,2,format='(a,t25,a)','07-DC-1A','PROG WRITE'
	  printf,2,format='(a,z2.2,t27,1h(,z2.2,1h,,z2.2,1h))' $
		,'07-BC-',ii(jj),ii(jj:jj+1)
	  printf,2,format='(a,z2.2)', '07-BC-',ii(jj+1)
	  if longf eq 'Y' then printf,2,format='(a,t25,a)','07-DC-11','BC EXEC'
	  endfor

next:
	iadd_old = iadd
	nbytes_old = nbytes
	endwhile

endit:
PRINTF,2,' '
printf,2,format='(a,t25,a)','07-DC-18','ENA DRCT DEC  (COMMAND DECODE)'
PRINTF,2,star

close,1
close,2
close,3
end
