Skip to content

Commit

Permalink
add timer example
Browse files Browse the repository at this point in the history
  • Loading branch information
erique committed Nov 7, 2022
1 parent 915e6cd commit 3bea547
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
34 changes: 34 additions & 0 deletions timer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
AS = $(VASM)
CC = $(VC)
LD = $(VLINK)

ASFLAGS := -quiet -Fhunk -kick1hunks -nosym -m68000 -no-opt
CFLAGS := -v -O2 -size -cpu=68060 -c99 -k -sc -DDEBUG
LDFLAGS := -sc -Bstatic -Cvbcc -nostdlib -Rshort -b amigahunk -s

TARGET := timer
OBJECTS := main.o timer.o
INCLUDE := $(wildcard *.h) $(wildcard *.s)

CRT0 = $(VBCC)/targets/m68k-amigaos/lib/startup.o
LDLIBS = -L$(VBCC)/targets/m68k-amigaos/lib -lvc

.EXPORT_ALL_VARIABLES:

.PHONY: clean

all: $(TARGET)

clean:
rm -f $(TARGET) $(OBJECTS) $(OBJECTS:.o=.asm) *.txt

$(TARGET) : $(OBJECTS) Makefile | buildenv
$(LD) $(LDFLAGS) $(CRT0) $(OBJECTS) $(LDLIBS) -o $@ -M$@.txt

%.o: %.c $(INCLUDE) Makefile | buildenv
$(CC) $(CFLAGS) -c $< -o $@

%.o: %.s Makefile | buildenv
$(AS) $< -o $@ -L $<.txt $(ASFLAGS) -I $(INC_ASM)

include ../Makefile.build
43 changes: 43 additions & 0 deletions timer/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdint.h>

#include <proto/exec.h>

#define NUMRUNS (10)
#define NUMITERATIONS (10000)

uint32_t runAsmCode(__reg("d0") uint32_t iterations);
uint32_t getCpuCycle() =
"\tmoveq.l\t#0,d0\n"
"\trept 100\n"
"\tsub.l\t$40400000,d0\t;read CPU clock\n"
"\tadd.l\t$40400000,d0\n"
"\tendr\n";

static uint32_t sample()
{
return getCpuCycle();
}

int main()
{
uint32_t result[NUMRUNS];

// Calibrate the cost of reading the cycle counter
Disable();
for (int i = 0; i < NUMRUNS; ++i)
result[i] = sample();
Enable();
for (int i = 0; i < NUMRUNS; ++i)
printf("REPT 100 took %d cycles\n", (int)result[i]);

// Measure the 5c/7instr loop
Disable();
for (int i = 0; i < NUMRUNS; ++i)
result[i] = runAsmCode(NUMITERATIONS);
Enable();
for (int i = 0; i < NUMRUNS; ++i)
printf("%d iterations took %d cycles\n", NUMITERATIONS, (int)result[i]);

return 0;
}
31 changes: 31 additions & 0 deletions timer/timer.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

; Example program
; This runs a 5-cycle code loop for N iterations (provided in D0),
; and then returns (in D0) the number of CPU cycles used.

PUBLIC _runAsmCode

_runAsmCode:

bset.b #1,$bfe001 ; trigger logic analyzer
tst.b $bfe001 ; delay due to SIO on the 060db
move.l $40400000,a0 ; sample cpu clock counter

.iter:
addx.l d1,d1 ; pOEP-only
addx.l d1,d1 ; pOEP-only
addx.l d1,d1 ; pOEP-only
addx.l d1,d1 ; pOEP-only

add.l d1,d1 ; pOEP|sOEP
subq.l #1,d0 ; pOEP|sOEP
bne.s .iter ; is predicted and folded, takes 0 cycles

sub.l $40400000,a0 ; sample cpu clock counter
bclr.b #1,$bfe001 ; reset logic analyzer

moveq.l #0,d0
sub.l a0,d0

rts

0 comments on commit 3bea547

Please sign in to comment.