All files / asm80-core/utils srec.js

100% Statements 279/279
93.84% Branches 61/65
100% Functions 6/6
100% Lines 279/279

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 2801x 1x 1x 1x 1x 1x 1x 1x 1x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 2109x 2109x 2109x 133x 133x 133x 133x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 2109x 2109x 130x 130x 130x 130x 130x 130x 2109x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1708x 1708x 1708x 1708x 4x 2x 2x 4x 1708x 1708x 1708x 1708x 1708x 1708x 1685x 1708x 23x 23x 23x 1683x 1683x 1683x 1708x 1683x 1683x 1708x 68x 68x 1683x 1683x 1708x 3x 1x 1x 1x 3x 3x 3x 3x 1683x 1683x 1708x 1119x 2109x 2109x 1119x 1119x 1119x 1708x 3x 3x 3x 2x 2x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 136x 136x 136x 136x 136x 136x 136x 136x 136x 136x 136x 136x 136x 136x 136x 136x 2146x 2146x 2146x 136x 136x 136x 136x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 2146x 2146x 132x 132x 132x 132x 132x 132x 2146x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1732x 1732x 1732x 1732x 6x 3x 3x 6x 1732x 1732x 1732x 1732x 1732x 1732x 1689x 1732x 26x 26x 26x 1686x 1686x 1686x 1732x 1686x 1686x 1732x 69x 69x 1686x 1686x 1732x 4x 1x 1x 1x 4x 4x 4x 4x 1686x 1686x 1732x 1121x 2146x 2146x 1121x 1121x 1121x 1732x 4x 4x 4x 3x 3x 4x 4x 4x 4x 4x 4x 1x  
import { toHex2, toHex4, toHex6 } from "./utils.js";
 
/**
 * Generate a single S-Record line (S1 format, 16-bit addresses)
 * @param {number} addr - Starting address for this line
 * @param {number[]} buffer - Array of data bytes
 * @returns {string} S-Record line
 */
const srecLine = (addr, buffer) => {
    let s = "S1";
    let len = buffer.length;
    let checksum = 0;
    s += toHex2(len + 3);       // Record length (data + address + checksum)
    s += toHex4(addr);          // 16-bit address
    
    // Calculate checksum from length and address bytes
    checksum = len + 3 + Math.floor(addr / 256) + Math.floor(addr % 256);
    
    // Add data bytes and update checksum
    for (let i = 0; i < buffer.length; i++) {
      s += toHex2(buffer[i]);
      checksum += buffer[i];
    }
    
    // Add one's complement checksum
    s += toHex2(256 - (checksum % 256));
    return s;
  };
 
/**
 * Generate S-Record format from data array (16-bit addresses)
 * @param {number} addr - Starting address
 * @param {number[]} dta - Data bytes
 * @returns {string} S-Record formatted string
 */
const makeSrec = (addr, dta) => {
    let inter = 0;
    let buffer = [];
    let ilen = 16;
    let out = "";
    
    for (let i = 0; i < dta.length; i++) {
      buffer.push(dta[i]);
      if (++inter === ilen) {
        // Flush buffer when line is full
        out += srecLine(addr, buffer) + "\n";
        buffer = [];
        inter = 0;
        addr += ilen;
      }
    }
    
    // Flush remaining bytes
    if (buffer.length) {
      out += srecLine(addr, buffer) + "\n";
    }
 
    return out;
  };
 
/**
 * Generate Motorola S-Record format output (S1/S9 format for 16-bit addresses)
 * @param {Object} result - Compilation result containing dump array
 * @param {string} [segment] - Optional segment filter (e.g., "CSEG", "DSEG")
 * @returns {string} Complete S-Record file content with S9 termination record
 */
export const isrec = (result, segment) => {
    let V=result.dump;
    let op;
    let addr = null;
    let len = 0;
    let segments = false;
    let dta = [];
    let out = "";
    
    for (let i = 0, j = V.length; i < j; i++) {
      op = V[i];
 
      // Process pragma directives
      if (op.opcode === ".PRAGMA") {
        if (op.params.length == 1 && op.params[0].toUpperCase() == "SEGMENT") {
          segments = true;
        }
      }
      
      // Skip conditional assembly blocks
      if (op.ifskip) continue;
      
      // Filter by segment if specified
      if (typeof op.segment !== "undefined" && typeof segment !== "undefined" && op.segment != segment) continue;
 
      if (segments) {
        if (!segment) segment = "CSEG"; // Default to code segment
        if (op.segment != segment) continue;
      }
 
      let opaddr = op.addr;
      // Adjust for phase directive
      if (op.phase) opaddr -= op.phase;
      
      // Set initial address
      if (opaddr !== undefined && len === 0) {
        addr = opaddr;
      }
      
      // Check for address discontinuity
      if (opaddr != addr + len) {
        if (len) {
          // Flush current data block
          out += makeSrec(addr, dta);
        }
        addr = opaddr;
        len = 0;
        dta = [];
      }
      
      // Add instruction bytes to data array
      if (op.lens) {
        for (let n = 0; n < op.lens.length; n++) {
          dta.push(op.lens[n]);
        }
        len += op.lens.length;
        continue;
      }
    }
    
    // Flush final data block
    if (dta.length) {
      out += makeSrec(addr, dta);
    }
    
    // Add S9 termination record with entry point
    let ent = result.opts?result.opts.ENT: result.entry.address || 0;
    let checksum = 3 + Math.floor(ent / 256) + Math.floor(ent % 256);
    out += "S903" + toHex4(ent) + toHex2(255 - (checksum % 256));
    return out;
  };
 
/**
 * Generate a single S-Record line (S2 format, 24-bit addresses)
 * @param {number} addr - Starting address for this line
 * @param {number[]} buffer - Array of data bytes
 * @returns {string} S-Record line with 24-bit address
 */
const srecLine28 = (addr, buffer) => {
    let s = "S2";
    let len = buffer.length;
    let checksum = 0;
    s += toHex2(len + 4);       // Record length (data + 3-byte address + checksum)
    s += toHex6(addr);          // 24-bit address
    
    // Calculate checksum from length and all address bytes
    checksum =
      len +
      4 +
      Math.floor(addr / 65536) +          // High byte
      (Math.floor(addr / 256) % 256) +    // Middle byte
      Math.floor(addr % 256);             // Low byte
      
    // Add data bytes and update checksum
    for (let i = 0; i < buffer.length; i++) {
      s += toHex2(buffer[i]);
      checksum += buffer[i];
    }
    
    // Add one's complement checksum
    s += toHex2(255 - (checksum % 256));
    return s;
  };
 
/**
 * Generate S-Record format from data array (24-bit addresses)
 * @param {number} addr - Starting address
 * @param {number[]} dta - Data bytes
 * @returns {string} S-Record formatted string with 24-bit addresses
 */
  const makeSrec28 = (addr, dta) => {
    let inter = 0;
    let buffer = [];
    let ilen = 16;
    let out = "";
    
    for (let i = 0; i < dta.length; i++) {
      buffer.push(dta[i]);
      if (++inter === ilen) {
        // Flush buffer when line is full
        out += srecLine28(addr, buffer) + "\n";
        buffer = [];
        inter = 0;
        addr += ilen;
      }
    }
    
    // Flush remaining bytes
    if (buffer.length) {
      out += srecLine28(addr, buffer) + "\n";
    }
 
    return out;
  };
 
/**
 * Generate Motorola S-Record format output (S2/S8 format for 24-bit addresses)
 * @param {Object} result - Compilation result containing dump array
 * @param {string} [segment] - Optional segment filter (e.g., "CSEG", "DSEG")
 * @returns {string} Complete S-Record file content with S8 termination record
 */
  export const isrec28 = (result, segment) => {
    let V=result.dump;
    let ln;
    let op;
    let addr = null;
    let len = 0;
    let segments = false;
    let dta = [];
    let out = "";
    
    for (let i = 0, j = V.length; i < j; i++) {
      op = V[i];
      
      // Process pragma directives
      if (op.opcode === ".PRAGMA") {
        if (op.params.length == 1 && op.params[0].toUpperCase() == "SEGMENT") {
          segments = true;
        }
      }
 
      // Skip conditional assembly blocks
      if (op.ifskip) continue;
      
      // Filter by segment if specified
      if (typeof op.segment !== "undefined" && typeof segment !== "undefined" && op.segment != segment) continue;
 
      if (segments) {
        if (!segment) segment = "CSEG"; // Default to code segment
        if (op.segment != segment) continue;
      }
 
      let opaddr = op.addr;
      // Adjust for phase directive
      if (op.phase) opaddr -= op.phase;
      
      // Set initial address
      if (opaddr !== undefined && len === 0) {
        addr = opaddr;
      }
      
      // Check for address discontinuity
      if (opaddr != addr + len) {
        if (len) {
          // Flush current data block
          out += makeSrec28(addr, dta);
        }
        addr = opaddr;
        len = 0;
        dta = [];
      }
      
      // Add instruction bytes to data array
      if (op.lens) {
        for (let n = 0; n < op.lens.length; n++) {
          dta.push(op.lens[n]);
        }
        len += op.lens.length;
        continue;
      }
    }
    
    // Flush final data block
    if (dta.length) {
      out += makeSrec28(addr, dta);
    }
    
    // Add S8 termination record with 24-bit entry point
    let ent = result.opts?result.opts.ENT: result.entry.address || 0;
    let checksum = 3 + Math.floor(ent / 256) + Math.floor(ent % 256);
    out += "S804" + toHex6(ent) + toHex2(255 - (checksum % 256)) + "\n";
    return out;
  };