def _mul(instr, ram, cpu): if cpu.mq == 0: cpu.mq += 1 g._lda_ac(ram.data[instr[1]], cpu) # sets AC to 0 if ram.data[instr[1]] == 0 or ram.data[instr[2]] == 0: g._lda_ac(0, cpu) return if ram.data[instr[1]] == 1: g._lda_ac(ram.data[instr[2]], cpu) return if ram.data[instr[2]] == 1: g._lda_ac(ram.data[instr[1]], cpu) return cpu.mq += 1 if (cpu.mq <= ram.data[instr[2]]): old = cpu.ac g._lda_ac(cpu.ac + ram.data[instr[1]], cpu) # adds AC and addr2, stores in AC if g.LOG_CONSOLE: print('MUL called: ', old, ' + ', ram.data[instr[1]], ' = ', cpu.ac, '. (', cpu.mq, '/', ram.data[instr[2]], ' done)') _mul(instr, ram, cpu) else: cpu.mq = 0 return
def _div(instr, ram, cpu): if cpu.mq == 0: g._lda_ac(ram.data[instr[1]], cpu) # sends instr[1] value to AC if ram.data[instr[1]] == 0 or ram.data[instr[2]] == 0: return if ram.data[instr[1]] == 1: g._lda_ac(ram.data[instr[2]], cpu) return if ram.data[instr[2]] == 1: g._lda_ac(ram.data[instr[1]], cpu) return cpu.mq += 1 if (cpu.ac >= ram.data[instr[2]]): old = cpu.ac g._lda_ac(cpu.ac - ram.data[instr[2]], cpu) # reduces AC by addr2, stores in AC if g.LOG_CONSOLE: print('DIV called: ', old, ' - ', ram.data[instr[2]], ' = ', cpu.ac, '.', cpu.mq) _div(instr, ram, cpu) else: cpu.ac = cpu.mq - 1 # final number is times ran - 1 if g.LOG_CONSOLE: print('DIV result: ', cpu.ac, '.') cpu.mq = 0 return
def _pow(instr, ram, cpu): if g.LOG_CONSOLE: print('POW called.') if ram.data[instr[2]] == 0: g._lda_ac(1, cpu) # 0 returns 1 return if ram.data[instr[2]] == 1: g._lda_ac(ram.data[instr[1]], cpu) # 1 returns itself return if cpu.mq == 0: cpu.ac = ram.data[instr[1]] # load instr1 to AC cpu.mq += 1 if cpu.mq <= ram.data[instr[2]]: g._sum_ac(cpu.ac, cpu.ac, cpu) _pow(instr, ram, cpu) else: cpu.mq = 0
def _sqr(instr, ram, cpu): if g.LOG_CONSOLE: print('SQRT called.') if cpu.mq == 0: g._stabuf(128, instr[2] + 1, ram) # \base g._stabuf(0, instr[2] + 2, ram) # \y cpu.mq += 1 if cpu.mq <= 8: g._sumbuf(ram.data[instr[2] + 1], ram.data[instr[2] + 2], instr[2] + 2, ram) # \store to y if ((ram.data[instr[2] + 2] * ram.data[instr[2] + 2]) > ram.data[instr[1]]): # we could also use _pow() to do this, but it would require some payload # conversion that would be annoying to do, as _pow() takes a different set of instr g._stabuf((ram.data[instr[2] + 2] - ram.data[instr[2] + 1]), instr[2] + 2, ram) # \y -= base g._stabuf((ram.data[instr[2] + 1] / 2), instr[2] + 1, ram) # same issue as above # we could also use _div() but it would take some annoyance to do g._lda_ac(ram.data[instr[2] + 2], cpu) # store in AC _sqr(instr, ram, cpu) else: cpu.mq = 0
def _arr(instr, ram, cpu): if g.LOG_CONSOLE: print('ARRAY called.') if cpu.mq == 0: # using cpu.mq because we don't have access to other registers yet (TP2?) instr[3] = ram.data[instr[2]] # trying to fix an undefined behavior bug g._stabuf(ram.data[instr[2]], (instr[1] + 1), ram) # sets addr[1] as SIZEOF g._lda_ac(ram.data[instr[1] + 2], cpu) # set AC to address[n + 2], after __const and SIZEOF _chr(instr, ram, cpu) # store __const to start_addr (addr[0]) cpu.mq += 1 if ram.data[instr[2]] != instr[3]: ram.data[instr[2]] = instr[3] # I have no idea why this is needed tbh # but instr[2] keeps reseting itself to 0 print(ram.data[instr[2]]) if cpu.mq <= ram.data[instr[2]]: g._stabuf(0, cpu.ac, ram) # store 0 into addr[AC] g._sum_ac(cpu.ac, 1, cpu) # adds 1 to AC _arr(instr, ram, cpu) # calls itself recursively until it fills the array else: cpu.mq = 0
def _fib(instr, ram, cpu): if g.LOG_CONSOLE: print('FIB called.') if (ram.data[instr[1]] == 0 or ram.data[instr[1]] == 1): g._lda_ac(ram.data[instr[1]], cpu) return if ram.data[instr[1]] == -1: g._lda_ac(1, cpu) return if ram.data[instr[2]] == 0: g._stabuf(2, instr[2], ram) # make sure we start at 2 g._stabuf(1, instr[2] + 1, ram) # allocate another byte g._stabuf(0, instr[2] + 2, ram) # allocate another byte # we could use MQ for this but to be honest idk if that would be legitimate if ram.data[instr[2]] <= ram.data[instr[1]]: # count until limit g._lda_ac((ram.data[instr[2] + 2] + ram.data[instr[2] + 1]), cpu) g._stabuf(ram.data[instr[2] + 1], instr[2] + 2, ram) g._stabuf(cpu.ac, (instr[2] + 1), ram) g._stabuf((ram.data[instr[2]] + 1), instr[2], ram) _fib(instr, ram, cpu)
def _qrt(instr, ram, cpu): g._lda_ac(1597463007, cpu) # magic number constant 0x5F3759DF g._lda_ac((cpu.ac - (cpu.ac / 2)), cpu) # what the f**k? g._lda_ac((cpu.ac * 1.5 - ((ram.data[instr[1]] * 0.5) * cpu.ac * cpu.ac)))
def _xor(instr, ram, cpu): if g.LOG_CONSOLE: print('OR called: checking if ', ram.data[instr[1]], ' is not equal to ', ram.data[instr[2]], '.') if (ram.data[instr[1]] != ram.data[instr[2]]): g._lda_ac(1, cpu) # stores 1 (true) to AC
def _and(instr, ram, cpu): if g.LOG_CONSOLE: print('AND called: checking if ', ram.data[instr[1]], ' is equal to ', ram.data[instr[2]], '.') if (ram.data[instr[1]] == ram.data[instr[2]]): g._lda_ac(1, cpu) # stores 1 (true) to AC
def _smr(instr, ram, cpu): if g.LOG_CONSOLE: print('SMR called: checking if ', ram.data[instr[1]], ' is smaller than ', ram.data[instr[2]], '.') if (ram.data[instr[1]] < ram.data[instr[2]]): g._lda_ac(1, cpu)
def _bgr(instr, ram, cpu): if g.LOG_CONSOLE: print('BGR called: checking if ', ram.data[instr[1]], ' is bigger than ', ram.data[instr[2]], '.') if (ram.data[instr[1]] > ram.data[instr[2]]): g._lda_ac(1, cpu)
def _not(instr, ram, cpu): if g.LOG_CONSOLE: print('NOT called: reversing ', ram.data[instr[1]], '.') g._lda_ac((int(ram.data[instr[1]])), cpu) # loads to AC g._stabuf((-cpu.ac), instr[2], ram) # stores inverse to dest