22const _ = require ( 'lodash' ) ;
33const fs = require ( 'fs' ) ;
44const util = require ( 'util' ) ;
5- const { octal, oct6, fieldExtract} = require ( './util' ) ;
5+ const { octal, oct6, fieldExtract, maskForBit , fieldMask } = require ( './util' ) ;
66const { cramFields} = require ( './fields-model' ) ;
7- const { unusedCRAMFields, CR } = require ( './ebox' ) ;
7+ const { unusedCRAMFields, CRAMdefinitions , defineBitFields , EBOX , ConstantUnit } = require ( './ebox' ) ;
88
99
1010// List of field names in an array indexed by leftmost bit.
@@ -29,7 +29,7 @@ function decodeSplats(s) {
2929function joinSplats ( a ) {
3030 let w = 0n ;
3131
32- for ( let k = 0 , shift = 0n ; shift < 79n ; ++ k , shift += 16n ) {
32+ for ( let k = 0 , shift = 0n ; shift < 80n ; ++ k , shift += 16n ) {
3333 w = ( w << shift ) | BigInt ( a [ k ] ) ;
3434 }
3535
@@ -49,15 +49,15 @@ function decodeLines(lines) {
4949 switch ( recType ) {
5050 case 'Z' :
5151 const [ zWC , zAdr , zCount , zCksum ] = decoded ;
52- console . log ( `Zero ${ zWC } @${ octal ( zAdr ) } count=${ zCount } . cksum=${ zCksum } .` ) ;
52+ // console.log(`Zero ${zWC}@${octal(zAdr)} count=${zCount}. cksum=${zCksum}.`);
5353 break ;
5454
5555 case 'C' :
5656 case 'D' :
5757 const [ wc , adr , ...a ] = decoded ;
5858 const checksum = a . pop ( ) ;
5959 const dump = a . map ( ( w , x ) => oct6 ( w ) + ( x % 6 === 5 ? '\n ' : '' ) ) . join ( ',' ) ;
60- console . log ( `${ recType === 'C' ? 'CRAM' : 'DRAM' } ${ octal ( adr ) } : ${ dump } ` ) ;
60+ // console.log(`${recType === 'C' ? 'CRAM' : 'DRAM'} ${octal(adr)}: ${dump}`);
6161
6262 if ( recType === 'C' ) {
6363
@@ -69,7 +69,7 @@ function decodeLines(lines) {
6969 break ;
7070
7171 default :
72- console . log ( `Unknown recType: "${ recType } "` ) ;
72+ console . error ( `Unknown recType: "${ recType } "` ) ;
7373 break ;
7474 }
7575 } ) ;
@@ -78,27 +78,61 @@ function decodeLines(lines) {
7878}
7979
8080
81+ function maybeSymbolic ( field ) {
82+ const v = field . get ( ) ;
83+ return field . namesForValues [ v ] || octal ( v ) ;
84+ }
85+
86+
87+ const W = ConstantUnit ( { name : 'WORD' , bitWidth : 84 , value : 0 } ) ;
88+ defineBitFields ( W , CRAMdefinitions ) ;
89+
90+
8191function disassembleCRAMWord ( w , a ) {
92+ EBOX . reset ( ) ;
93+ W . value = w ;
8294 const wSplit = _ . range ( 0 , 84 , 12 ) . reduce ( ( cur , s ) => cur . concat ( [ octal ( fieldExtract ( w , s , s + 11 , 84 ) ) ] ) , [ ] ) ;
83- CR . value = w ;
8495
85- const dis = fieldsByBit . filter ( names => ! unusedCRAMFields . includes ( names [ 0 ] ) ) . map ( names => {
86- const name = names [ 0 ] ;
87- const field = cramFields [ name ] ;
88- return `${ names [ 0 ] } /${ fieldExtract ( w , field . s , field . e , 84 ) } ` ;
89- } ) . join ( ', ' ) ;
96+ // Compute mask of CRAM word bits we need to disassemble. This is
97+ // gradually zeroed as we proceed disassembling this word.
98+ let remainingMask = fieldMask ( 0 , 83 , 84 ) ;
99+ unusedCRAMFields . forEach ( name => {
100+ const { s, e} = cramFields [ name ] ;
101+ remainingMask &= ~ fieldMask ( s , e , 84 ) ;
102+ } ) ;
103+
104+ // Clear bits for J since we do it at the end
105+ remainingMask &= ~ fieldMask ( W . J . s , W . J . e ) ;
106+
107+ const pieces = [ ] ;
108+ const [ ad , ada , adb , ar , arx ] = 'AD,ADA,ADB,AR,ARX' . split ( / , / ) . map ( f => W [ f ] . get ( ) ) ;
109+
110+ if ( ad || ada || adb ||
111+ ar === W . AR . AD || ar === W . AR [ 'AD*2' ] || ar === W . AR [ 'AD*.25' ] ||
112+ arx === W . ARX . AD || arx === W . ARX [ 'ADX*2' ] || arx === W . ARX . ADX || arx === W . ARX [ 'ADX*.25' ] )
113+ {
114+ pieces . push ( `AD/${ maybeSymbolic ( W . AD ) } ` ) ;
115+
116+ pieces . push ( `ADA/${ maybeSymbolic ( W . ADA ) } ` ) ;
117+ pieces . push ( `ADB/${ maybeSymbolic ( W . ADB ) } ` ) ;
118+ if ( ar ) pieces . push ( `AR/${ maybeSymbolic ( W . AR ) } ` ) ;
119+ if ( arx ) pieces . push ( `ARX/${ maybeSymbolic ( W . ARX ) } ` ) ;
120+ }
121+
122+ // Save the best for last
123+ pieces . push ( `J/${ octal ( W . J . get ( ) ) } ` ) ;
90124
91- console . log ( `U ${ octal ( a ) } : ${ wSplit } ${ dis } ` ) ;
125+ console . log ( `U ${ octal ( a ) } : ${ wSplit } ${ pieces . join ( ', ' ) } ` ) ;
92126}
93127
94128
95129function main ( ) {
96- const [ ramA , ramB ] = [ 'kl10-source/eboxa.ram' , 'kl10-source/eboxa.ram' ]
130+ const [ klx , ramA , ramB ] = [ 'kl10-source/klx.ram' , 'kl10-source/eboxa.ram' , 'kl10-source/eboxa.ram' ]
97131 . map ( fileName => decodeLines ( fs . readFileSync ( fileName )
98132 . toString ( )
99133 . split ( / \n / ) ) ) ;
100134
101- ramA . forEach ( ( w , a ) => disassembleCRAMWord ( w , a ) ) ;
135+ klx . forEach ( ( w , a ) => disassembleCRAMWord ( w , a ) ) ;
102136}
103137
104138
0 commit comments