;+
; NAME:
;	EDIT_FULL_LOAD
; PURPOSE:
;	Applies patches to loaded image of BCS micro and calculates
;	the checksum
; CALLING SEQUENCE:  
;	.run edit_full_load
; INPUTS:
;	loads FULL_LOAD.DAT as requested
; OPTIONAL INPUTS:
;	patch file names
; OUTPUTS:
;	Lists contents of patch
;	Identifies differences following the patch
;	Calculates checksum
; OPTIONAL OUTPUTS:
;
; RESTRICTIONS:
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
;	RDB  Nov 91	Written
;	RDB  29-Jul-92	Cosmetic stuff
;	RDB  17-May-94	Split out the mem_checksum function
;			Used call to rd_full_load; $BCS_PATCH logical
;-


;	programme to edit the BCS microprocessor memory load

ans = ''
print,' '
read,'Load MEMORY array from FULL_LOAD.DAT [def=N] ',ans
ans = strupcase(strmid(ans,0,1))

if ans ne 'Y' then goto,skip_full_load

;;openr,1,'load_dir:full_load.dat',/block
;;full_memory = 0
;;qq = assoc(1,bytarr(16384))
;;full_memory = qq(0)
;;close,1
;;print,'MEMORY array read from FULL_LOAD.DAT'

rd_full_load,full_memory

skip_full_load:

checksum = mem_checksum(full_memory)

print,' '
read,'Edit the file? ',ans
ans = strupcase(strmid(ans,0,1))
if ans ne 'Y' then goto,skip_edit

old_load = full_memory		;save unpatched image

;	patch list files available 
next_file:
spawn,'dir patch_dir:ptch_7a08*.hex;0'
print,' '

;	get filename, check it exists and open it
filopnerr:
print,' '
filnam=''
read,'Enter filename: ',filnam
filename = concat_dir('$BCS_PATCH',filnam)
if (findfile(filename))(0) eq "" then begin
   print,'*** File not found *** ',filename
   goto,filopnerr
endif

on_ioerror,filopnerr
openr,1,filename
print,' '

;	header record
ss=''
;;ans=''
;;read,'Is there a header record? ',ans
;;if strupcase(strmid(ans,0,1)) eq 'Y' then readf,1,ss

;	the data...
nbyt=0
iadd=0L
ii=intarr(16)

while not eof(1) do begin
	point_lun,-1,poss
	readf,1,ss &  ndata = (strlen(ss) -11)/2
	if ndata eq 0 then goto, endit

	ii = indgen(ndata)
	point_lun,1,poss
	readf,1,nbyt,iadd,ii,format='(x,Z2,Z4,2x,16Z2)'
	print,format='(x,Z2.2,2x,Z4.4,3x,16Z3.2)',nbyt,iadd,ii
	if nbyt ne ndata then print,'NBYT/NDATA ERROR',nbyt,ndata
	if iadd eq '1ff0'x then goto,endit
	full_memory(iadd) = ii(*)
	print,format='(x,i2,x,a,2x,Z4)' $
	  ,nbyt,'bytes patched starting at address',iadd
	endwhile

endit:
  close,1
  print,' '
  print,'PATCHES applied from ',filnam
  print,' '

  diffs = where(old_load ne full_memory)
;print,diffs
  if (n_elements(diffs) gt 1) or $
	((n_elements(diffs) eq 1) and (diffs(0) ne -1)) then begin
    print,'Differences between OLD and NEW load are at:'
    print,diffs,format='(8Z8.4)'
  endif else print,'There were no differences between OLD and NEW loads'

  print,' '
  checksum = mem_checksum(full_memory)

print,' '
read,'Further edits? ',ans
ans = strupcase(strmid(ans,0,1))
if ans eq 'Y' then goto, next_file

;?? save new version?
;?? what name

skip_edit:

end
