One of the things I'm terrible at is counting cpu cycles.

So let's figure out how many cycles it takes to do the system beep, and while we're at it figure out what the cpu clock is.

How can we get the totalcycles?
./mame64 apple2e -plugin console -debug

Just ask the debugger to print the totalcycles.

Immediately after boot, if we ask the debugger for totalcycles we get:

Code
>print totalcycles
3

so how to get that value into a variable in lua?

Code
function getcycles() 
  dbg = manager:machine():debugger()
  dbg:command("print totalcycles") 
  lastline = dbg.consolelog[#dbg.consolelog] 
  cycles = tonumber(lastline,16) 
  return cycles
end

and a single line version to paste at the lua console:

Code
function getcycles() dbg = manager:machine():debugger() dbg:command("print totalcycles")   lastline = dbg.consolelog[#dbg.consolelog]   cycles = tonumber(lastline,16)   return cycles end
so let's set a watchpoint and then go:

Code
dbg = manager:machine():debugger()
dbg:command("wpset c030,1,rw")
dbg:command("g")

so we've hit the first click of the speaker so get the time and the cycles:

Code
a = emu.time(); cyclesa = getcycles()

so let's get the next click:

Code
dbg:command("g")
b = emu.time(); cyclesb = getcycles()

print(cyclesb-cyclesa)

546

and remember that it's a half cycle (since the c030 inverts the speaker) so we multiply by 2


and if we do a little math we can get the system clock:


Code
[MAME]> print(1/((b-a)/(cyclesb-cyclesa)))
1021800.0000005
[MAME]> print((cyclesb-cyclesa)*2)
1092
[MAME]> print(1021800/1092)
935.71428571429
[MAME]> 

and sure enough the cpu clock corresponds exactly to the source code that defines the cpu frequency

Code
static MACHINE_CONFIG_DERIVED( apple2ee, apple2e )
	MCFG_CPU_REPLACE("maincpu", M65C02, 1021800)        /* close to actual CPU frequency of 1.020484 MHz */
	MCFG_CPU_PROGRAM_MAP(apple2e_map)
MACHINE_CONFIG_END


One of the things that baffled me for a bit was why the system clock for the Atari 2600 would come up as not matching the source code exactly:


Code
MAME debugger version 0.191 (mame0191-183-g346a935839-dirty)
Currently targeting a2600 (Atari 2600 (NTSC))
[MAME]> function getcycles() dbg = manager:machine():debugger() dbg:command("print totalcycles")   lastline = dbg.consolelog[#dbg.consolelog]   cycles = tonumber(lastline,16)   return cycles end


[MAME]> a = emu.time(); acycles=getcycles()
[MAME]> 
3
[MAME]> dbg:command("step")
[MAME]> b = emu.time(); bcycles=getcycles()
[MAME]> 
5

print((bcycles-acycles)/(b-a))
1193181.0000007

print((bcycles-acycles)/(b-a)*3)
3579543.0000022

print(3579545/3)
1193181.6666667


so why doesn't 3579543 match up with 3579545? Finally I realized that there's some rounding taking place that converts (3579543/3) = 1193181.6666667 into 1193181.0 because the machines take an integer value for the frequency and that accounts for the discrepancy (bye bye fractional part).

Code

#define MASTER_CLOCK_NTSC   3579545

static MACHINE_CONFIG_START( a2600 )
	/* basic machine hardware */
	MCFG_CPU_ADD("maincpu", M6507, MASTER_CLOCK_NTSC / 3)

Last edited by Golden Child; 11/22/17 02:47 PM.