-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
128 lines (113 loc) · 3.54 KB
/
helpers.js
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
let input_prg = []
let cur_input_idx = 2
function sys_read_line() {
if(cur_input_idx >= input_prg.length || input_prg[cur_input_idx + 1] == 0) { var_C4int = 0; return }
var_C4int = 1
var_B1 = input_prg[cur_input_idx + 2] + input_prg[cur_input_idx + 3] * 256
}
function sys_chrget() {
return int_sys_chrget(true)
}
function sys_chrget3() {
return int_sys_chrget(false)
}
function int_sys_chrget(ignore_spaces) {
var_C2int = 0
var_C3int = 0
let chr = 0
do {
var_C1int++
chr = input_prg[cur_input_idx + var_C1int]
if(chr == 0) { // end of line
var_Cint = 0
var_C2int = 1
var_C3int = 0x1f
cur_input_idx += var_C1int + 1
return
}
} while(ignore_spaces && chr == 0x20)
var_Cint = chr
if(chr == 0x3a) { var_C3int = 0x1f }
return
}
function copy_extra_data() {
// called at beginning of pass2 to copy any trailing extra data to result file
cur_input_idx+=2
if(cur_input_idx < input_prg.length) {
c64_print(`Copying ${input_prg.length - cur_input_idx} bytes trailing the BASIC part to output.`)
}
while(cur_input_idx < input_prg.length) {
write_pcode()
var_Istr = String.fromCharCode(input_prg[cur_input_idx])
cur_input_idx++
}
}
function sys_find_in_array() {
let arr = 0
if(var_C5int == 10427) {
arr = var_Gintarr
} else if(var_C5int == 12036) {
arr = var_Hintarr
} else if(var_C5int == 7218) {
arr = var_Dintarr
} else { console.log("ERROR in sys_findinarray") }
let search_for = var_C6int
let size = var_C7int
var_C5int = -1
let i = 0
while(i < size) {
if(arr[i] == search_for) {
var_C5int = i
return
}
i++
}
}
function float2string(f) {
let f_buf = new Float64Array([f])
let b_buf = new Uint32Array(f_buf.buffer)
let sign_1b = b_buf[1] >> 31
let exponent_11b = ((b_buf[1] >> 20) & 0x7ff) - 1023
let fraction_31b = ((b_buf[1] & 0xfffff) << 11) | (b_buf[0] >>> 21) // implicit leading 1
if((b_buf[0] >>> 19 & 3) >= 1) fraction_31b++ // rounding
let c64_b1 = exponent_11b + 129
let c64_b2 = (sign_1b << 7) + (fraction_31b >> 24)
let c64_b3 = (fraction_31b >> 16) & 0xff
let c64_b4 = (fraction_31b >> 8) & 0xff
let c64_b5 = fraction_31b & 0xff
return String.fromCharCode(c64_b1) + String.fromCharCode(c64_b2) + String.fromCharCode(c64_b3) + String.fromCharCode(c64_b4) + String.fromCharCode(c64_b5)
}
// adjust var_T1int to change length of runtime/offset of P-code
// comment out the following line to exclude runtime from output
for(let ch of runtime) { pass2_result = pass2_result + String.fromCharCode(ch) }
let p2_cur_p1_result_idx = 0
function sys_p2_char_read() {
var_C1int = pass1_result.charCodeAt(p2_cur_p1_result_idx)
p2_cur_p1_result_idx++
}
function sys_p2_char_write() {
pass2_result = pass2_result + String.fromCharCode(var_C1int)
}
function c64_parse_float(s) {
if(s == ".") return 0
return parseFloat(s)
}
let scr_output = ""
function c64_print(s) {
scr_output = scr_output + s + "\n"
}
if(typeof document !== 'undefined') {
document.run_it = main
console.log("document.run_it() to start")
} else {
const fs = require('fs')
input_prg = fs.readFileSync(process.argv[2], null)
console.log(`Reading file ${process.argv[2]}, length ${input_prg.length}`)
c64_print = console.log
let start = performance.now()
main()
console.log(`Running took ${(performance.now() - start)>>>0}ms`)
console.log(`Result length is ${pass2_result.length} bytes`)
// fs.writeFileSync('test_p1_result.bin', pass1_result, {encoding: "ascii"});
fs.writeFileSync(process.argv[3], pass2_result, {encoding: "ascii"});
}