Hello everyone, I am trying to work on emulating 6502 processor (very early 
stage, basically just started) and I am following tutorial in C++. I know Nim 
is not fully object-oriented, so how could I solve following please?

I have a problem with recursive dependency, specifically where Olc6502 object 
in one file has ref of Bus inside and Bus object in another file has ref to 
Olc6502 obj:

olc6502.nim file
    
    
    import bus
    type
      
      Olc6502* = object of RootObj
        bus : ref Bus ###############################
    
    proc read*(cpu: Olc6502, x: uint16) : uint8 =
      result = read(cpu.bus, x, false)
    
    
    Run

bus.nim file 
    
    
    import algorithm
    import olc6502
    
    type
      Ram = array[64*1024, uint8]
      Bus* = object of RootObj
    # Devieces on bus
        cpu* : Olc6502 ##############################
        ram* : Ram
    
    # Bus read & write
    proc newBus* : ref Bus =
      result.new()
      result.ram.fill(0x00'u8)
    
    proc write*(bus: var Bus, address: uint16, data: uint8) : void =
      if address in 0x0000'u16 .. 0xFFFF'u16:
        bus.ram[address] = data
    
    proc read*(bus: ref Bus, address: uint16, bReadOnly : bool = false) : uint8 
=
      if address in 0x0000'u16 .. 0xFFFF'u16:
        result = bus.ram[address]
      result = 0x00'u8
    
    
    Run

Don't mind mistakes in my code, I just want to get rid of that recursive 
dependency for now. If there is a way to create something like header files in 
Nim (however I didn't find anything about it) maybe that would help. When I 
write it as a single file it gives no error, but I want to keep it separated 
preferably. Also I am trying to use refs instead of pointers, but I don't know 
if it's completely avoidable. 

Reply via email to