;+
pro wrt_ld_cmd, lun, segment, offset, buf
;
; Creates a control file to load data contained in "buf".
;     Parameters:
;        lun     - logical unit on which the output file is open
;        segment - DEP Load Address Segment
;        offset  - DEP Load Address Offset
;        buf     - An array containing data to be loaded
;
;-

bufinf = size (buf)
len = bufinf(bufinf(0)+2)
op_buf = buf
op_len = len 
if ((op_len mod 4) ne 0) then begin
    op_len = ((len / 4) + 1) * 4
    filbuf = intarr(op_len - len)
    op_buf = [buf(*), filbuf]
endif
print, len, op_len
;stop

n = 1l
while (n le op_len) do begin
    if ((n mod 24) eq 1) then begin
        printf, lun, segment, offset, format = $
            '(/"tcb mbdpml ", 2("0x",z4.4, 1x), "\")
    endif 
    printf, lun, op_buf(n-1), format = '($, "0x", z4.4, " ")'
    if ((n mod 8) eq 0) and (n lt op_len) and ((n mod 24) ne 0) then $
        printf, lun, '\'
    offset = offset + 2 
    n = n + 1
endwhile
printf, lun, ' '

return
end
;............................................
;
; Program to generate and load an Arbitrary Weighted Bin List
;
;
; Generate Address List

addr_list = lonarr(40000)

for j = 0, 63 do begin
    for i = 0, 63 do begin
        addr = ((i+32) * 8l) + (((j+32) * 8) * 1024L) 
        k = i + (j * 64)
        addr_list(2 * k) = addr and 'ffff'x
        addr_list((2 * k) + 1) = ishft (addr, -16)
    endfor
endfor


; Generate Weight Table

a = [25,50,75,100,125,150,175,200,225,250,225,200,175,150,125,100,75,50,25]
;a = intarr(19) + 1
b = a
x = lonarr(19,19)
for i = 0, 18 do begin
    for j = 0, 18 do begin
        x(j,i) = a(j) * long(b(i))
    endfor
endfor


; Generate Bin List 
hdr_buf = ['01A2'x, 1024, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
wt_tbl_addr = '00000410'x
wtadr_hi = ishft (wt_tbl_addr, -16)
wtadr_lo = wt_tbl_addr and 'ffff'x
x_len = 19
y_len = 19
adr_list = '0000057C'x
adr_list_len = 128
shift_code = 12
segment = '2000'x 
offset = 0l

fmt = '(11x, '
fmt = fmt + '2("0x", z4.4, 1x), '
fmt = fmt + '" 4  4 ", '
fmt = fmt + '2("0x", z4.4, 1x), '
fmt = fmt + '" 64  0x000F")'


openw, lun, 'ld_awbin.src', /get_lun
printf, lun, '# File: ld_awbin.src"
printf, lun, '#'
printf, lun, 'program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"'

printf, lun, format = '("#"/"#"/"# AWBIN List Header:")'
wrt_ld_cmd, lun, segment, offset, hdr_buf

; Load Bin List
printf, lun, format = '(2("#"/), "# Arbitrarily Weighted Bin List:")'
for i = 0, 127 do begin
    printf, lun, offset, format = '("tcb mbdpml 0x2000 0x", z4.4, 3x, "\")'
    adrlst_addr_hi = ishft (adr_list, -16)
    adrlst_addr_lo = adr_list and 'ffff'x
    printf, lun, wtadr_lo, wtadr_hi, adrlst_addr_lo, adrlst_addr_hi, $
        format = fmt
    offset = offset + 16
    adr_list = adr_list + 128
endfor

;Load Weight Table
printf, lun, format = '(2("#"/), "# Weight Table:")'
wrt_ld_cmd, lun, segment, offset, x

; Load Address List
;
printf, lun, format = '(2("#"/), "# Address List:")'
wrt_ld_cmd, lun, segment, offset, addr_list(0:8191)

printf, lun, '#'
printf, lun, 'tcb mbiptbl 0x09 0 0x8800 0 0 0x2000 0x1a2 10620 10620 \'
printf, lun, '224 0 0 0x1800 0 0 0x2000 0x5000 0'
printf, lun, 'return'
close, lun
free_lun, lun

end

