#!/bin/bash
#
# ROM image generator for 2732 eprom adapter for DEC M9301 Bootstrap
#
# Charles Dickman, chd@chdickman.com, 2/4/2006
#

# rom image files are intel hex format and based at address 0
                          # diagnostics are 512 bytes (01000 or 0x100)
diag_1134=23-248F1-1983   # diagnostics for all but the 11/60 and 11/70

                          # boot roms are 128 bytes (0200 or 0x80)
rom_DL=23-751a9           # DL     RL01, RL02 cartridge disks
rom_DM=23-752a9           # DM     RK06, RK07 cartridge disks 
rom_DX=23-753a9           # DX     RX01 floppy disk, single density
rom_DP=23-755a9           # DP     RP02/RP03 disk packs
rom_DB=23-755a9           # DB     Massbus disk packs RP04, RP05, RP06, RM02, RM03
rom_DK=23-756a9           # DK     RK03, RK05 cartridge disks
rom_DT=23-756a9           # DT     TU55, TU56 DECtape on TC11 DECtape control
rom_MM=23-757a9           # MM     Massbus tape drives TU10, TU45, TU77, TE16
rom_MT=23-758a9           # MT     TS03, TU10, TE10 tape drives
rom_DS=23-759a9           # DS     RS03, RS04
rom_TT=23-760a9           # TT     ASR33 paper tape reader (low speed)
rom_PP=23-760a9           # PP     PC05 paper tape reader (high speed)
rom_CT=23-761a9           # CT     TU60 DECcassette
rom_MS=23-764a9           # MS     TS04, TS11, TU80 tape drives
rom_DD=23-765a9           # DD     TU58 DECtape II
rom_DU=23-767a9           # DU     MSCP disks, including RAxx drives on UDA50
rom_DY=23-811a9           # DY     RX02 floppy disk, double density
rom_MU=23-e39a9           # MU     TMSCP tapes, including TK50, TU81

image=${1-myimage}        # name for the image files

highmask=0x01             # 0x01 for the M9301
lowmask=0xFE              # 0xFE for the M9301

# eprom layout
#
#    pdp-11          eprom       contents
# 165000:165777 - 0000h:01FFh - diagnostic
# 173000:173177 - 0200h:027Fh - rom0
# 173200:173377 - 0280h:02FFh - rom1
# 173400:173577 - 0300h:037Fh - rom2
# 173600:173777 - 0380h:03FFh - rom3
#  note eprom addresses are before splitting
#
# This is then repeated 8 times in the eprom with each
# bank selected by DIP switches. The image file is then
# split into two halves: even bytes and odd bytes.

# generate the contents of one bank
bank()
{
  # $1 = diag, $2 = rom0, $3 = rom4, $4 = rom2, $5 = rom3, $6 = image

  # convert M9312 rom images to DEC binaries
# the standard m9312 does only converts 64 words and does not correctly
# handle the diagnostics
#  for f in $1
#    do
#      m9312 -u $f.hex $f.abs
#    done

  # convert M9312 rom images to DEC binaries
  for f in $2 $3 $4 $5
    do
      m9312 -u $f.hex $f.abs
    done
  
  # convert DEC binaries to hex
  #   the m9312 utility places the binaries at 173000 (0xF600)
  for f in $1
    do
      srec_cat $f.abs -dec-binary -offset -0xF600 -o $f.tmp.hex -intel
    done
  
  # convert DEC binaries to hex
  #   the m9312 utility places the binaries at 173000 (0xF600)
  #   the boot console start address is also replaced
  #     base boot address must be ORed with 0776 so that switch
  #     register is able to select boot address
  #     ex: to boot into 165000 rom, 0xEBFE must be programmed
  #     high byte is at 0xF615, low byte at 0xF614
  for f in $2 $3 $4 $5
    do
      srec_cat $f.abs -dec-binary \
               -exclude 0xF615 0xF616 \
               -fill 0xEB 0xF615 0xF616 \
               -exclude 0xF614 0xF615 \
               -fill 0xFE 0xF614 0xF615 \
               -offset -0xF600 -o $f.tmp.hex -intel
    done
  
  # join the files together
  srec_cat $1.tmp.hex -intel \
           $2.tmp.hex -intel -offset +0x200 \
           $3.tmp.hex -intel -offset +0x280 \
           $4.tmp.hex -intel -offset +0x300 \
           $5.tmp.hex -intel -offset +0x380 \
           -o $6.hex -intel 
}

# create an images for each bank
bank $diag_1134 $rom_DX $rom_MT $rom_DK $rom_DL $image.0
bank $diag_1134 $rom_DY $rom_MT $rom_PP $rom_DD $image.1
bank $diag_1134 $rom_DY $rom_MT $rom_DK $rom_DL $image.2
bank $diag_1134 $rom_DY $rom_MT $rom_DU $rom_MU $image.3
#bank $diag_1134 $rom_DY $rom_MT $rom_DU $rom_MU $image.4
#bank $diag_1134 $rom_DY $rom_MT $rom_DU $rom_MU $image.5
#bank $diag_1134 $rom_DY $rom_MT $rom_DU $rom_MU $image.6
#bank $diag_1134 $rom_DY $rom_MT $rom_DU $rom_MU $image.7

# join the bank images together
srec_cat $image.0.hex -intel -offset 0x0000 \
         $image.1.hex -intel -offset 0x0400 \
         $image.2.hex -intel -offset 0x0800 \
         $image.3.hex -intel -offset 0x0C00 \
         -o $image.hex -intel

# split the image into a high and low byte
srec_cat $image.hex -intel -split 2 0 -o $image.low.hex -intel
srec_cat $image.hex -intel -split 2 1 -o $image.high.hex -intel

# apply a complement mask
srec_cat $image.low.hex -intel -XOR $lowmask -o $image.low.hex -intel
srec_cat $image.high.hex -intel -XOR $highmask -o $image.high.hex -intel

# adjust HEX format for burner
mv $image.low.hex low
mv $image.high.hex high
grep -v :0[24] low > $image.low.hex
grep -v :0[24] high > $image.high.hex




