|
1 |
| -import { filter, map, type Observable, share, tap } from 'rxjs' |
| 1 | +import { filter, map, type Observable, share } from 'rxjs' |
2 | 2 |
|
3 |
| -import { type Bus, Control, type Signals } from '../bus/bus' |
| 3 | +import type { Bus } from '../bus/bus' |
| 4 | + |
| 5 | +export enum MemoryOperationType { |
| 6 | + READ = 'READ', |
| 7 | + WRITE = 'WRITE', |
| 8 | +} |
| 9 | + |
| 10 | +export interface MemoryOperation { |
| 11 | + type: MemoryOperationType |
| 12 | + data: number |
| 13 | + address: number |
| 14 | +} |
4 | 15 |
|
5 | 16 | export class Memory {
|
6 | 17 | // TODO: use shared constants
|
7 | 18 | private readonly data = new Uint8Array(0x100)
|
8 | 19 |
|
9 |
| - private readonly read$: Observable<Signals> |
10 |
| - private readonly write$: Observable<Signals> |
| 20 | + readonly read$: Observable<MemoryOperation> |
| 21 | + readonly write$: Observable<MemoryOperation> |
11 | 22 |
|
12 | 23 | constructor(
|
13 | 24 | private readonly bus: Bus,
|
14 | 25 | ) {
|
15 |
| - this.read$ = this.bus.signals$.pipe( |
16 |
| - filter((signals) => signals.control === Control.MEMORY_READ), |
17 |
| - tap(this.read), |
| 26 | + const control$ = this.bus.control$.pipe( |
| 27 | + filter((control) => control.MREQ), |
18 | 28 | share(),
|
19 | 29 | )
|
20 | 30 |
|
21 |
| - this.write$ = this.bus.signals$.pipe( |
22 |
| - filter((signals) => signals.control === Control.MEMORY_WRITE), |
23 |
| - tap(this.write), |
| 31 | + this.read$ = control$.pipe( |
| 32 | + filter((control) => control.RD), |
| 33 | + map(this.read), |
| 34 | + share(), |
| 35 | + ) |
| 36 | + |
| 37 | + this.write$ = control$.pipe( |
| 38 | + filter((control) => control.WR), |
| 39 | + map(this.write), |
24 | 40 | share(),
|
25 | 41 | )
|
26 | 42 |
|
27 | 43 | this.read$.subscribe()
|
28 | 44 | this.write$.subscribe()
|
29 | 45 | }
|
30 | 46 |
|
31 |
| - private read = (signals: Signals) => { |
32 |
| - this.bus.put({ |
33 |
| - data: this.data[signals.address], |
34 |
| - control: Control.CLOCK_IDLE, |
35 |
| - }) |
| 47 | + private read = (): MemoryOperation => { |
| 48 | + const address = this.bus.address$.getValue() |
| 49 | + const data = this.data[address] |
| 50 | + this.bus.data$.next(data) |
| 51 | + return { |
| 52 | + type: MemoryOperationType.READ, |
| 53 | + data, |
| 54 | + address, |
| 55 | + } |
36 | 56 | }
|
37 | 57 |
|
38 |
| - private write = (signals: Signals) => { |
39 |
| - this.data[signals.address] = signals.data |
40 |
| - this.bus.put({ |
41 |
| - control: Control.CLOCK_IDLE, |
42 |
| - }) |
| 58 | + private write = (): MemoryOperation => { |
| 59 | + const address = this.bus.address$.getValue() |
| 60 | + const data = this.bus.data$.getValue() |
| 61 | + this.data[address] = data |
| 62 | + return { |
| 63 | + type: MemoryOperationType.WRITE, |
| 64 | + data, |
| 65 | + address, |
| 66 | + } |
43 | 67 | }
|
44 | 68 |
|
45 |
| - get data$() { |
46 |
| - return this.write$.pipe(map(() => this.getData())) |
| 69 | + getData = (): number[] => { |
| 70 | + return Array.from(this.data) |
47 | 71 | }
|
48 | 72 |
|
49 |
| - getData() { |
50 |
| - return Array.from(this.data) |
| 73 | + subscribeData = (onDataChange: (() => void)): (() => void) => { |
| 74 | + const subscription = this.write$.subscribe(onDataChange) |
| 75 | + return () => subscription.unsubscribe() |
51 | 76 | }
|
52 | 77 |
|
53 |
| - load(data: Uint8Array, offset: number) { |
| 78 | + load(data: Uint8Array, offset: number): void { |
54 | 79 | this.data.set(data, offset)
|
55 | 80 | }
|
56 | 81 |
|
57 |
| - reset() { |
| 82 | + reset(): void { |
58 | 83 | this.data.fill(0)
|
59 | 84 | }
|
60 | 85 | }
|
0 commit comments