On a C64 PAL model, the VIC chip processes through 312 lines where each lines takes 63 clock cycles to display (including some time for horizontal and vertical blanking for returning the raster beam). So, if a timer counting every clock cycle is once synchronized to the beginning of rasterline 0, then the timer value
t tells us the number of the current line with
(t MOD 63)*8 yields the X coordinate. Alternatively, the current line can be read from VIC register at $D012 and $D011 (9th bit). Note that the raster beam will be long gone from this position when the CPU is done with the calculation, but it might be helpful to record the timer value at a given point in a program and calculate where the rasterline had been in an offline debug session. The CIA 6526 latch value for a PAL-synchronized timer is
In the VICE emulator, the problem is much easier solved because the monitor allows to stop execution and provides access to a CPU cycle counter which starts at value
0 when VICE is started or a reset takes place. This event exactly coincides with the raster beam starting at the beginning of line 0.
The cycle counter can be printed with the command
stopwatch in the monitor. Thus, with
c being the cycle counter value, the current X position of the beam can be estimated with
(c MOD 63)*8 and the current raster line with
int((c MOD 19656) / 63). The line value can also be obtained by checking the VIC registers with
io d000 in the monitor.
Image explaining timing of rasterbeam (Figure by Dustlayer based on Michael Steil's Ultimate Commodore 64 Talk) Note that the coordinate system for calculation of line and X position has its origin in the border.