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 | 277x 277x 277x 277x 277x 277x 96x 96x 96x 88x 88x 88x 4x 88x 88x 6x 6x 2x 2x 2x 2x 2x 2x 1x 4x 4x 4x 4x 47x 47x 28x 28x 28x 28x 28x 1x 1x 8x 2x 2x 2x 2x 2x 19x 13x 13x 19x 19x 7x 7x 7x 7x 7x 7x 7x | /**
* BBC Micro Analogue to Digital Converter (ADC)
* Handles input from analogue sources through various channels
*
* @typedef {import('./analogue-source.js').AnalogueSource} AnalogueSource
*/
export class Adc {
/**
* Create a new ADC
* @param {object} sysvia - System VIA interface
* @param {object} scheduler - Scheduler for timing operations
*/
constructor(sysvia, scheduler) {
this.sysvia = sysvia;
this.task = scheduler.newTask(this.onComplete.bind(this));
this.status = 0x40;
this.low = 0x00;
this.high = 0x00;
// Initialize channel sources (one source per channel)
this.channelSources = [null, null, null, null];
}
/**
* Reset the ADC state
*/
reset() {
this.status = 0x40;
this.low = 0x00;
this.high = 0x00;
}
/**
* Set the source for a specific channel
* @param {number} channel - The channel number (0-3)
* @param {AnalogueSource} source - The source to assign to the channel
* @returns {boolean} True if the assignment was successful
*/
setChannelSource(channel, source) {
Iif (channel < 0 || channel > 3) {
console.error(`ADC: Invalid channel number: ${channel}`);
return false;
}
// Dispose of the old source if one exists and is different
const oldSource = this.channelSources[channel];
if (oldSource && oldSource !== source) {
oldSource.dispose();
}
// Set the channel source
this.channelSources[channel] = source;
return true;
}
/**
* Get the source for a specific channel
* @param {number} channel - The channel number (0-3)
* @returns {AnalogueSource|null} The source for the channel or null if none
*/
getChannelSource(channel) {
Iif (channel < 0 || channel > 3) {
return null;
}
return this.channelSources[channel];
}
/**
* Clear the source for a specific channel
* @param {number} channel - The channel number (0-3)
* @returns {boolean} True if successful
*/
clearChannelSource(channel) {
Iif (channel < 0 || channel > 3) {
console.error(`ADC: Invalid channel number: ${channel}`);
return false;
}
const source = this.channelSources[channel];
Eif (source) {
source.dispose();
this.channelSources[channel] = null;
}
return true;
}
/**
* Clear all sources
*/
clearSources() {
// Dispose and clear all channel sources
for (let i = 0; i < 4; i++) {
const source = this.channelSources[i];
Eif (source) {
source.dispose();
this.channelSources[i] = null;
}
}
}
snapshotState() {
const scheduler = this.task.scheduler;
return {
status: this.status,
low: this.low,
high: this.high,
taskOffset: this.task.scheduled() ? this.task.expireEpoch - scheduler.epoch : null,
};
}
restoreState(state) {
this.status = state.status;
this.low = state.low;
this.high = state.high;
this.task.cancel();
if (state.taskOffset !== null) {
this.task.schedule(state.taskOffset);
// Conversion in progress: CB1 should be high (set by write(), cleared by onComplete())
this.sysvia.setcb1(true);
}
}
/**
* Read from the ADC registers
* @param {number} addr - The address to read from
* @returns {number} The value at the address
*/
read(addr) {
switch (addr & 3) {
case 0:
return this.status;
case 1:
return this.high;
case 2:
return this.low;
default:
break;
}
return 0x40;
}
/**
* Write to the ADC control register
* @param {number} addr - The address to write to
* @param {number} val - The value to write
*/
write(addr, val) {
if ((addr & 3) !== 0) return;
// 8 bit conversion takes 4ms whereas 10 bit conversions take 10ms, according to AUG
this.task.cancel();
this.task.schedule(val & 0x08 ? 20000 : 8000);
this.status = (val & 0x0f) | 0x80;
this.sysvia.setcb1(true);
}
/**
* Called when the ADC conversion is complete
*/
onComplete() {
const channel = this.status & 0x03;
const source = this.channelSources[channel];
const val = source ? source.getValue(channel) : 0x8000;
this.status = (this.status & 0x0f) | 0x40 | ((val >>> 10) & 0x03);
this.low = val & 0xff;
this.high = (val >>> 8) & 0xff;
this.sysvia.setcb1(false);
}
}
|