11import { inject } from 'di-wise'
2- import { filter , map , type Observable , share } from 'rxjs'
2+ import { filter , map , type Observable } from 'rxjs'
33
44import { Bus } from '../bus/bus'
55
@@ -15,38 +15,35 @@ export interface MemoryOperation {
1515}
1616
1717export class Memory {
18- private readonly data = new Uint8Array ( 0x100 )
18+ private readonly buffer = new Uint8Array ( 0x100 )
19+
20+ private dataChangeHandlers = new Set < ( ) => void > ( )
21+
22+ private notifyDataChange = ( ) => {
23+ this . dataChangeHandlers . forEach ( ( handler ) => handler ( ) )
24+ }
1925
2026 readonly read$ : Observable < MemoryOperation >
2127 readonly write$ : Observable < MemoryOperation >
2228
2329 private bus = inject ( Bus )
2430
2531 constructor ( ) {
26- const control$ = this . bus . control$ . pipe (
27- filter ( ( control ) => control . MREQ ) ,
28- share ( ) ,
29- )
30-
31- this . read$ = control$ . pipe (
32- filter ( ( control ) => control . RD ) ,
32+ this . read$ = this . bus . control$ . pipe (
33+ filter ( ( control ) => control . MREQ && control . RD ) ,
3334 map ( this . read ) ,
34- share ( ) ,
3535 )
36-
37- this . write$ = control$ . pipe (
38- filter ( ( control ) => control . WR ) ,
36+ this . read$ . subscribe ( )
37+ this . write$ = this . bus . control$ . pipe (
38+ filter ( ( control ) => control . MREQ && control . WR ) ,
3939 map ( this . write ) ,
40- share ( ) ,
4140 )
42-
43- this . read$ . subscribe ( )
44- this . write$ . subscribe ( )
41+ this . write$ . subscribe ( this . notifyDataChange )
4542 }
4643
4744 private read = ( ) : MemoryOperation => {
4845 const address = this . bus . address$ . getValue ( )
49- const data = this . data [ address ]
46+ const data = this . buffer [ address ]
5047 this . bus . data$ . next ( data )
5148 return {
5249 type : MemoryOperationType . READ ,
@@ -58,7 +55,7 @@ export class Memory {
5855 private write = ( ) : MemoryOperation => {
5956 const address = this . bus . address$ . getValue ( )
6057 const data = this . bus . data$ . getValue ( )
61- this . data [ address ] = data
58+ this . buffer [ address ] = data
6259 return {
6360 type : MemoryOperationType . WRITE ,
6461 data,
@@ -67,19 +64,21 @@ export class Memory {
6764 }
6865
6966 getData = ( ) : number [ ] => {
70- return Array . from ( this . data )
67+ return Array . from ( this . buffer )
7168 }
7269
73- subscribeData = ( onDataChange : ( ( ) => void ) ) : ( ( ) => void ) => {
74- const subscription = this . write$ . subscribe ( onDataChange )
75- return ( ) => subscription . unsubscribe ( )
70+ subscribe = ( onDataChange : ( ( ) => void ) ) : ( ( ) => void ) => {
71+ this . dataChangeHandlers . add ( onDataChange )
72+ return ( ) => this . dataChangeHandlers . delete ( onDataChange )
7673 }
7774
78- load ( data : Uint8Array , offset : number ) : void {
79- this . data . set ( data , offset )
75+ load ( data : ArrayLike < number > , offset : number ) : void {
76+ this . buffer . set ( data , offset )
77+ this . notifyDataChange ( )
8078 }
8179
8280 reset ( ) : void {
83- this . data . fill ( 0 )
81+ this . buffer . fill ( 0 )
82+ this . notifyDataChange ( )
8483 }
8584}
0 commit comments