UNPKG

asm80

Version:
2 lines 4.23 kB
#!/usr/bin/env node var __cwd=process.cwd(),ASM=require("./asm.js"),Monolith=require("./monolith.js"),path=require("path"),LFS=require("./lfsnode.js"),hextools=require("./hextools.js"),btoa=require("btoa"),atob=require("atob"),pjson=require("./package.json"),corejson=require("./core.json"),program=require("commander");program.version(pjson.version+"-core"+corejson.version).usage("[options] <file>").option("-o, --output <file>","Output file name").option("-t, --type <type>","Output type [default: hex] - hex, srec, com (for CP/M), sna, tap (for ZX Spectrum), prg (for C64), bin").option("-n, --nolist","Suppress listing").option("-m, --machine <type>","Processor type (see below)").on("--help",function(){if(console.log(" Machine types:"),console.log(""),Monolith)for(var e in Monolith)console.log(" - "+e);console.log(""),console.log("See https://maly.github.io/asm80-node/ for further docs")}).parse(process.argv),program.args.length||program.help();var asmType=function(e){var o="unknown";switch(path.extname(e).toUpperCase()){case".A80":o="I8080";break;case".A08":o="I8008";break;case".A68":o="M6800";break;case".A18":o="CDP1802";break;case".A09":o="M6809";break;case".A65":o="C6502";break;case".816":o="C65816";break;case".Z80":o="Z80"}return o},fn=path.resolve(__cwd,program.args[program.args.length-1]),asmtype=asmType(fn),data=LFS.load(program.args[program.args.length-1]);program.machine&&(asmtype=program.machine.toUpperCase());var vxx=[null,null],mpath=path.parse(fn),root=mpath.dir;switch(ASM.fileGet(function(e,o){var a=path.resolve(root,e);return LFS.load(a,o)}),asmtype){case"I8080":vxx=ASM.compile(data,Monolith.I8080);break;case"I8008":vxx=ASM.compile(data,Monolith.I8008);break;case"C6502":vxx=ASM.compile(data,Monolith.C6502);break;case"C65816":vxx=ASM.compile(data,Monolith.C65816);break;case"Z80":vxx=ASM.compile(data,Monolith.Z80);break;case"M6800":vxx=ASM.compile(data,Monolith.M6800);break;case"CDP1802":vxx=ASM.compile(data,Monolith.CDP1802);break;case"M6809":vxx=ASM.compile(data,Monolith.H6309),ASM.PRAGMAS.indexOf("6309")<0&&(vxx=ASM.compile(data,Monolith.M6809));break;default:console.log("Unrecognized ASM type"),process.exit(-1)}if(vxx[0]){var err=vxx[0];err.msg?(console.error("ERROR ",err.msg),console.error("at line ",err.s.numline),console.error(">>> ",err.s.line)):console.log(vxx[0]),process.exit(-1)}var otype="hex";if(program.type)switch(program.type.toUpperCase()){case"SREC":otype="srec";break;case"HEX":otype="hex";break;case"PRG":otype="prg";break;case"COM":otype="com";break;case"SNA":otype="sna";break;case"TAP":otype="tap";break;case"BIN":otype="bin";break;default:console.log("Unknown output type"),process.exit(-1)}var npath=path.parse(fn);npath.ext="."+otype,delete npath.base;var outdata,vx=vxx[1];if(outdata="srec"===otype?ASM.srec(vx[0]):ASM.hex(vx[0]),program.output&&(npath.base=program.output),ASM.PRAGMAS.indexOf("SEGMENT")>=0){LFS.save(path.format(npath),outdata);var mseg=ASM.hex(vx[0],"DSEG");npath.ext=".dseg.hex",mseg.length>11&&LFS.save(path.format(npath),mseg),npath.ext=".eseg.hex",mseg=ASM.hex(vx[0],"ESEG"),mseg.length>11&&LFS.save(path.format(npath),mseg),npath.ext=".hex"}if("prg"===otype&&("C6502"!==asmtype&&console.log("Warning: PRG is for Commodore C64, it should be compiled for 6502 CPU"),ASM.ENT||console.log("Please specify the entry point (use .ENT directive)"),outdata=hextools.hex2prg(outdata,ASM.ENT)),"com"===otype&&("I8080"!==asmtype&&"Z80"!==asmtype&&console.log("Warning: COM is for CP/M, it should be compiled for 8080/Z80 CPU"),outdata=hextools.hex2com(outdata,ASM.ENT)),"sna"===otype&&("Z80"!==asmtype&&console.log("Warning: SNA is for ZX Spectrum, it should be compiled for Z80 CPU"),outdata=hextools.makeSNA(vx[0],ASM.ENT)),"tap"===otype&&("Z80"!==asmtype&&console.log("Warning: TAP is for ZX Spectrum, it should be compiled for Z80 CPU"),outdata=hextools.makeTAP(vx[0],ASM.ENT)),"bin"===otype){var hex=ASM.hex(vx[0]),mFrom=0,mTo=65535;ASM.BINFROM&&(mFrom=ASM.BINFROM),ASM.BINTO&&(mTo=ASM.BINTO),outdata=hextools.hex2bin(hex,mFrom,mTo)}if(LFS.save(path.format(npath),outdata),!program.nolist){var lpath=path.parse(path.format(npath));lpath.ext=".lst",delete lpath.base;var lst=ASM.lst(vx[0],vx[1]);LFS.save(path.format(lpath),lst)}