;-
;+
; NAME:
;	DUMP_COMPARE
; PURPOSE:
;	Extracts a microprocessor memory dump from the BDA file and 
;	compare it with the specified memory load, within the 
;	specified address range.
; CALLING SEQUENCE:  
;	.run dump_compare
; INPUT:
;	YODAT should just be used to extract the BCS DHK data (option 
;	-888 and Y to DHK). 
;	Select required record numbers from range indicated (determined 
;	from DP_SYNC data). If not already extracted, the data is 
;	extracted from the BDA file. 
;	The comparison should be normally made between 0 and 35FF (hex).
; OUTPUT:
;	List of non-compares printed to screen
; OPTIONAL OUTPUT:
;	If hardcopy of non-compares is required, set TTL=2 and rerun
;	the programme. Produces file "mem_comp.lis" on current directory
; SIDE EFFECTS:
;	If required data is not already extracted an "SS" vector of the 
;	required records is created and the data is then extracted (it 
;	is not necessary to re-run YODAT).
; HISTORY
;	RDB, 1990, with enhancements
;	rdb, 16-Feb-92	Updated for per-orbit files
;			Does not print if large no. of non-compares
;			Extracts the parameter area
;	rdb  22-Feb-92  Extract data from BDA file if not present.
;			Syncs with start of dump.
;	RDB  10-May-92	Added code to allow for byte slipped dump
;	RDB  21-Jun-92	Capitalzed $DIR_BCS_MICRO for Unix
;			Used call to CONCAT_DIR
;	RDB  23-Jul-92	Allow bad block in middle of dump
;	RDB  29-Jan-94	Added extra comments
;	RDB  11-May-94  Also extract area that holds default seqns and flops
;			Use rd_full_load to get memory load
;	RDB  17-May-94	Added comparison to plan_area image
;       RDB   1-Sep-94  Small correction
;-

print,' '
print,'   Programme to compare the memory dumped in telemetry by the'
print,'   BCS micro with a load file of some sort'
print,' '
print,'   ** Run after extracting the DHK data with YODAT **'
print,' '
print,'   If you want a listing, set TTL=2 and rerun programme'
print,' '
print,' '

break_file,infil(0),disk_log,dir,filnam,ext,fver
ans=''

read,'* Does dump need to be extracted from telemetry data: ',ans
ans = strupcase(strmid(ans,0,1))
if ans ne 'Y' then begin
	rd_st = 0
	filnam = ''
	frtime = bytarr(6,1)
	goto, qcmpit
	endif

;>>	extract the memory dump from PHDATA array

istarec = 0L
print,'Data File is:   ',filnam+ext
print,' '



;	  Find the records with a micro dump in them
iidx = where(dp_sync.dp_mode eq 22)
if iidx(0) eq -1 then begin
   print,'*** No DUMP in this file ***'
   goto, endit
endif
iidx = iidx(1:*)
print,'Dump is in DP_SYNC records:'
print,iidx
iidx_sta = 0  &  iidx_stp = 0

print,' '
Read,'* Enter start and stop record: ',iidx_sta,iidx_stp
print,' '

jjdx = tim2dset(roadmap,dp_sync(iidx_sta:iidx_stp))
print,' '
print,'This is equivalent to ROADMAP records:'
print,jjdx
print,' '
ss = jjdx		;vector of required records

;	determine whether the data is already present
qqdata = 1
if (n_elements(data)/4096 lt n_elements(jjdx))  $
    or n_elements(dset_arr) eq 0  then qqdata = 0
if n_elements(dset_arr) gt 0 then begin
    if (min(dset_arr.dset) gt jjdx(0))  $
       or (max(dset_arr.dset) lt jjdx(n_elements(jjdx)-1)) then qqdata = 0
endif

;;msg = '*** Data not in extract section, rerun TEST_RD using SS option ***'
msg = '*** Data not in extract section - extracting data using SS vector ***'
if qqdata eq 0 then begin
  print,msg
;;  goto, endit
;;;  dset_arr = ss2dset(ss)
  dset_arr = ss2dset(indgen(max(ss)-min(ss)+1)+min(ss))	;allow for bad data
  rd_bda,infil,dset_arr,index,data,roadmap
endif


;;idx2list,dp_sync(iidx_sta),['time','day'],fxtime
int2ex,dp_sync(iidx_sta).time,dp_sync(iidx_stp).day,tarr
fxtime = fmt_tim(tarr)
print,' '
print,'Dump starts at:  ',fxtime

;	  Extract the dump (allowing for partial extraction from data area)
;;dump = data(0:4095,min(jjdx-min(dset_arr.dset)):max(jjdx-min(dset_arr.dset)))
dump = data(0:4095,[jjdx-min(ss)])		;allow for bad data
help,dump
print,' '

;	find which block the dump starts at
ihead = long(byte(['af'x,'c3'x,'40'x,'01'x]),0,1) & ihead = ihead(0)
jblock = where(ihead eq long(dump,0,n_elements(dump)/4))
byte_shift = 0
if jblock(0) eq -1 then begin
   print,'*** No match, trying one byte shift ***'
   jblock = where(ihead eq long(dump,1,n_elements(dump)/4-1))
   byte_shift = 1
endif

if jblock(0) ge 0 then begin
   jblock = jblock(0)/1024
;;  if jblock ne 0 then begin
;;    print,'** Dump starts at record',fix(jblock)
;;    dump = dump(*,jblock:*)
;;  endif
   dump = dump(jblock*4096 + byte_shift:(jblock+4)*4096-1 + byte_shift)
endif else begin
   print,'*** Dump not found in data ***'
   goto, finit
endelse

;if n_elements(where(dump(*,0) eq 'AA'x) eq 4096) then begin
;  print,'** First record is fill **'
;  dump = dump(*,1:*)
;  endif

print,' '
print,'Dump extraction complete'
print,' '

;>>	Should we do a compare?

qcmpit:
read,'* Compare Dump with a memory load: ',ans
ans = strupcase(strmid(ans,0,1))
if ans ne 'Y' then goto, endit

;>>	If so, get a memory load for the comparison

full_memory = 0
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
;;get_lun,dlun
;;openr,dlun,concat_dir('$DIR_BCS_MICRO','full_load.dat'),/block
;;qq = assoc(dlun,bytarr(16384))
;;full_memory = qq(0)
;;close,dlun
;;qq = 0		;clear array
;;print,'MEMORY array read from FULL_LOAD.DAT'

rd_full_load,full_memory
goto, cmpit

skip_full_load:
;	try from intel hex file
read,'* Load MEMORY array from Intel Hex file [def=N]: ',ans
ans = strupcase(strmid(ans,0,1))
if ans ne 'Y' then goto,skip_load
read_intelhex,memory_load
goto, cmpit

skip_load:
print,' '
print,'Array assumed to be defined on entry in MEMORY_LOAD!!'

cmpit:

;>>	Now do the comparison

print,' '
istartd = 0L & iendc = 0L
;;print,'* Enter start address of DUMP'
;;read,'$(Z4.4)',istartd
;;print,'* Enter end address of COMPARISON'
;;read,'$(Z4.4)',iendc

trange = ['0000','35FF']
tstartd = ''  &  tendc = ''
read,'* Enter start address of DUMP     [def = "',trange(0),'"]: ',tstartd
if tstartd ne '' then trange(0) = tstartd
read,'* Enter end address of COMPARISON [def = "',trange(1),'"]: ',tendc
if tendc ne '' then trange(1) = tendc
irange = lonarr(2)
reads,trange,irange,format='(z4.4)'
istartd = irange(0)  &  iendc = irange(1)

itop = iendc - istartd  	;number of comparisons	
print,'$(3Z8.4)',istartd,iendc,itop

;	if read from FULL_LOAD.DAT, extract the memory equivalent to dump
if n_elements(full_memory) gt 1 then $
	memory_load = full_memory(istartd:iendc)
if n_elements(memory_load) gt 8192L*2 then begin
	memory_load = memory_load(istartd:iendc)
	print,'Array MEMORY_LOAD is large and has been contracted'
	endif

;	  Write to file, or to screen if not defined!
if n_elements(ttl) eq 0 then ttl=-1 
if ttl gt 0 then openw,ttl,'mem_comp.lis'


;***		Output banner

printf,ttl,' '
printf,ttl,string('BCS Memory Dump Comparison','(30x,a)')
printf,ttl,' '
printf,ttl,'File used is:     ',strupcase(filnam+ext)
printf,ttl,'DP_SYNC Records used:     ',string(iidx_sta,' to',iidx_stp,'(i6,a,i6)')
printf,ttl,'DATA Records used:        ',string(min(jjdx),' to',max(jjdx),'(i6,a,i6)')
printf,ttl,' '
printf,ttl,'Dump started at:  ',fxtime
if byte_shift gt 0 then printf,ttl,'** Dump was shifted in telemetry **'
printf,ttl,' '
printf,ttl,'Comparison done between  ',string(istartd,'(Z4.4)'), $
	'  and  ',string(iendc,'(Z4.4)') + 'H'

nonc = where(memory_load(0:itop) ne dump(0:itop))
n_nonc = n_elements(nonc)
if (n_nonc eq 1) and (nonc(0) eq -1) then goto,no_nonc

;qnc = strarr(itop+1)		;array of non compare flags
;qnc(*) = ' '
;for j=0,n_nonc-1 do qnc(nonc(j)) = '*'

index8 = all_vals(nonc/8)
add8 = index8*8		;address of lines where there are non comps.
nell = n_elements(add8)

if n_nonc le 50 then begin
printf,ttl,'$(i5,2x,a)',n_nonc,'Non-Compares found at:'
printf,ttl,'$(8Z8.4)', nonc+istartd
endif else begin
printf,ttl,'$(i5,2x,a)',n_nonc,'Non-Compares found'
endelse

if n_nonc gt 200 then begin
  print,'*** Number of non-compares is very large ***'
  read,'* Do you want to print? ',ans
  ans = strupcase(strmid(ans,0,1))
  if ans ne 'Y' then goto,p_area
  endif

printf,ttl,' '
printf,ttl,' '
printf,ttl,string('ADDR','LOAD','DUMP','(2x,a,18x,a,31x,a)')
for j=0,nell-1 do begin
  qnc = strarr(8)		;array of non compare flags
  qnc(*) = ' '
  sqadd = add8(j)
  qqxx = where((nonc ge sqadd) and (nonc le sqadd+7))
  qnc(nonc(qqxx)-sqadd) = '*'
  nss = ''
  for k=0,7 do $
	nss = nss + string(dump(add8(j)+k),qnc(k),'(Z2.2,a)') + ' '
    printf,ttl,'$(Z6.4,3x,8Z4.2,5x,a)' $
	,add8(j)+istartd, memory_load(add8(j):add8(j)+7), nss
endfor
printf,ttl,' '
goto, p_area	;;endit

no_nonc:
printf,ttl,' '
printf,ttl,' '
printf,ttl,'          ****  No Non-Compares found  ****'
printf,ttl,' '
printf,ttl,' '


; 	dumps paramaters area
p_area:

plan_area    = reform(dump('3800'x:'39ff'x))     ;main area
default_area = reform(dump('323c'x:'3265'x))     ;default seqns and flops
                                                 ;seqn0,seqn1,flop0,flop1

read,'* Do you wish to compare the Paramater Area?: ',ans
ans = strupcase(strmid(ans,0,1))
if ans ne 'Y' then goto, endit


;;Checksum = mem_checksum(plan_area)	;IDL routine of same name!!!

printf,ttl,' '
printf,ttl,' '
printf,ttl,'Dump of PLAN area'
;;printf,ttl,' '
;;printf,ttl,'Filename is:   ',filnam+ext
printf,ttl,' '
printf,ttl,plan_area,format='(3(x,8(x,Z2.2)))'

;	compare with current image 
rd_plan_area,plan_image				;read in current image
nonc = where(plan_image ne plan_area)
n_nonc = n_elements(nonc)
if (n_nonc ge 1) and (nonc(0) ne -1) then begin
  print,'*** No. of non-compares in plan area:',n_nonc
  print,nonc
endif


endit:
if n_elements(ttl) gt 0 then begin
   if ttl gt 0 then begin
      close,ttl
      yesnox,'* Print the dump file: ',ans,'yes'
      if ans then dprint,'mem_comp.lis'
   endif
endif

finit:
end
