wokwi / avr8js

Arduino (8-bit AVR) simulator, written in JavaScript and runs in the browser / Node.js
https://blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
MIT License
462 stars 73 forks source link

Extracting registers for visualization #77

Closed SebastianZug closed 3 years ago

SebastianZug commented 3 years ago

Hello Uri,

I want to access individual registers (timers, counters) etc in a tutorial to then show the "internal" mechanisms in a graph. How can I then access the memory from a js function to read the status?

Greetings !

Sebastian

urish commented 3 years ago

Hi Sebastian, that's a great idea! I also want to add this in wokwi.com some time soon.

Basically, it goes like this:

CPU registers

Peripheral registers (timers, etc)

You can get their memory space address by looking at the configuration object you passed to the constructor. So for example, if you want the current value for Timer/Counter0, that's the TCNT0 register, and its address will reside in timer0Config.TCNT. So you can access its value by cpu.data[timer0Config.TCNT].

Another example: accessing DDRB (DDR of PORTB) at cpu.data[portBConfig.DDR].

Note that you can also use this method to change the value registers if you need. The only exception is cpu.SREG, which is a read-only property. You can overcome it by writing directly to cpu.data[0x5f];