Kaynağa Gözat

Started working a BCD helper, and an implementation of the MOS CIA chip.

master
Bryan Miller 5 yıl önce
ebeveyn
işleme
a162b4ae88
2 değiştirilmiş dosya ile 168 ekleme ve 0 silme
  1. +129
    -0
      src/MOSCIA.js
  2. +39
    -0
      src/utils/bcd.js

+ 129
- 0
src/MOSCIA.js Dosyayı Görüntüle

@@ -0,0 +1,129 @@
const Clock = require('common/clock.js');

function Num2BCD(n){
let v = (n < 9) ? Math.floor(n / 10) : 0;
v = (v << 4) | (n - (v * 10));
return v;
}

class MOSCIA{
constructor(){
this.__regsel = 0;

this.__CTA = 0;
this.__CTB = 0;

this.__timerA = 0;
this.__timerALatch = 0;
this.__timerB = 0;
this.__timerBLatch = 0;

this.__TODLatch = 0;
this.__TODTick = 0;
this.__10ths = 0;
this.__sec = 0;
this.__min = 0;
this.__hours = 0;
}

get RS(){return this.__regsel;}
set RS(rs){this.__regsel = rs & 0x0F;}

get DATA(){
let val = 0;
switch(this.__regsel){
case 0x00: // Peripheral Data A
break;
case 0x01: // Peripheral Data B
break;
case 0x02: // Data Direction Reg A
break;
case 0x03: // Data Direction Reg B
break;
case 0x04: // Timer A Low
break;
case 0x05: // Timer A High
break
case 0x06: // Timer B Low
break;
case 0x07: // Timer B High
break;
case 0x08: // 10th of Sec Reg
this.__TODLatch = 0;
return Num2BCD(this.__10ths); break;
case 0x09: // Seconds Reg
return Num2BCD(this.__sec); break;
case 0x0A: // Minutes Reg
return Num2BCD(this.__min); break;
case 0x0B: // Hours AM/PM Reg
this.__TODLatch = 1;
return Num2BCD(this.__hours); break;
case 0x0C: // Serial Data
break;
case 0x0D: // Interrupt Control
break;
case 0x0E: // Control Timer Reg A
break;
case 0x0F: // Control Timer Reg B
break;
}
return val;
}

set DATA(d){
switch(this.__regsel){
case 0x00: // Peripheral Data A
break;
case 0x01: // Peripheral Data B
break;
case 0x02: // Data Direction Reg A
break;
case 0x03: // Data Direction Reg B
break;
case 0x04: // Timer A Low
break;
case 0x05: // Timer A High
break
case 0x06: // Timer B Low
break;
case 0x07: // Timer B High
break;
case 0x08: // 10th of Sec Reg
break;
case 0x09: // Seconds Reg
break;
case 0x0A: // Minutes Reg
break;
case 0x0B: // Hours AM/PM Reg
break;
case 0x0C: // Serial Data
break;
case 0x0D: // Interrupt Control
break;
case 0x0E: // Control Timer Reg A
break;
case 0x0F: // Control Timer Reg B
break;
}
}

get CNT(){return 0;}
set CNT(c){}

get SP(){return 0;}
set SP(b){}

get TOD(){return 0;}
set TOD(b){
if (b <= 0 || this.__TODLatch > 0){return;}
this.__TODTick += 1;
if (this.__TODTick === 6){
this.__TODTick = this.__CTA & 0x01;
}
}
}


module.exports = MOSCIA;



+ 39
- 0
src/utils/bcd.js Dosyayı Görüntüle

@@ -0,0 +1,39 @@


function isValid(bcd, n){
for (let i=0; i < n; i++){
if ((bcd & 0xF) > 9)
return false;
bcd = bcd >> 4;
}
return true;
}

function dec2BCD(b, n){
;
}

function BCD2Dec(bcd, n){
// TODO: Fix bug!
let v = 0
for (let i=0; i < n; i++){
bcd = bcd >> (i*4);
v += (bcd & 0x0F) * Math.pow(10,i);
}
return v;
}

function add(a, b, n){
let c = 0;
let v = 0;
for (i=0; i < (n*4); i += 4){
let nibA = (a >> i) & 0x0F;
let nibB = (b >> i) & 0x0F;
let r = nibA + nibB + c;
c = (r > 9) ? 1 : 0;
r = (c == 1) ? (r + 6) & 0x0F : r;
v = v | (r << i);
}
return v;
}


Yükleniyor…
İptal
Kaydet