English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 28 June 2011, 18:53   #1
DBAlex
 
Posts: n/a
Using timer.device in C (VBCC)

Hi everyone,

Yesterday I installed VBCC, and today I have been trying to write a program to test the computation speed of a string function I wrote (Just for fun, It's not really a serious program).

To do this I needed to get the time before and after the compututation, first I tried time.h (from the C Standard Library). I found out that with VBCC clock() returns -1 with VBCC. (This is standard compliant but useless for me!).

So I did a google search and found this thread on EAB: http://eab.abime.net/showthread.php?p=761327

I tried the sample by Thomas Rapp, here is my actual code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include <devices/timer.h>
#include <proto/timer.h>
#include <clib/timer_protos.h>

struct Library *TimerBase;

static struct timeval startTime;

void startup(){
    GetSysTime(&startTime);
}

ULONG getMilliseconds(){
    struct timeval endTime;

    GetSysTime(&endTime);
    SubTime(&endTime,&startTime);

    return (endTime.tv_secs * 1000 + endTime.tv_micro / 1000);
}

char* realloc_strcat(char *s1, char * s2){
    int len = (strlen(s1) + strlen(s2));
    s1 = (char *) realloc(s1, len + 1);
    strncat(s1,s2,len);
    return s1;
}

int main(int argc, char * argv[]){
    // Start timer
    startup();

    // Initialise string
    int i;
    char* str;
    str = (char *) malloc(7);
    strcpy(str,"Hello ");
    
    // Call timer (1)
    printf("Time 1: %lu\n",getMilliseconds());    
    
    // Computation
    for(i=0; i<100; i++){
        str = realloc_strcat(str,"Appending string...!)");
    }
    
    // Call timer (2)
    printf("Time 2: %lu\n",getMilliseconds());    

}
I struggled at first to find out what to include! There is no mention of the includes needed to run the code in the EAB thread! And I couldn't find any examples of using timer.device.

It compiles fine, however I got errors about TimerBase, so I added in that code.

I compile with vbcc like so: vc strings.c -o strings -c99 -lmieee

However, when I run the code it crashes straight away! (I get the "Suspend", "Reboot" requester). The crash is caused by calling the startup() function.

Can anyone shed any light on why this may not be working?

Or provide simple examples of using timer.device to get the current milliseconds using C!

Finally, can anyone reccomend a nice Amiga programming guide/book using C for absolute beginners?

Thanks for any help!

Alex.
 
Old 28 June 2011, 20:56   #2
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
You need to open the timer.device using OpenDevice() and then pick the device base from the IORequest structure you pass to OpenDevice() and store this in TimerBase.

If this is your first taste of the easy-to-use AmigaOS APIs then you may throw up in the corner over there --->

EDIT: here's an example for you:

Code:
#include <stdio.h>
#include <clib/timer_protos.h>
#include <clib/exec_protos.h>

struct Device* TimerBase;
static struct IORequest timereq;


static ULONG timer()
{
  static struct timeval t;
  struct timeval a, b;

  GetSysTime(&a);
  b = a;
  SubTime(&b, &t);
  t = a;

  return b.tv_secs*1000 + b.tv_micro/1000;
}


int main()
{
  OpenDevice("timer.device", 0, &timereq, 0);
  TimerBase = timereq.io_Device;

  timer();

  for(int n = 200; n--;)
    printf("%i ", n);

  ULONG t = timer();

  printf("\n%u ms\n", t);

  CloseDevice(&timereq);
  return 0;
}

Last edited by Leffmann; 28 June 2011 at 21:11.
Leffmann is offline  
Old 28 June 2011, 22:10   #3
DBAlex
 
Posts: n/a
Quote:
Originally Posted by Leffmann View Post
You need to open the timer.device using OpenDevice() and then pick the device base from the IORequest structure you pass to OpenDevice() and store this in TimerBase.

If this is your first taste of the easy-to-use AmigaOS APIs then you may throw up in the corner over there --->

EDIT: here's an example for you:

Code:
#include <stdio.h>
#include <clib/timer_protos.h>
#include <clib/exec_protos.h>

struct Device* TimerBase;
static struct IORequest timereq;


static ULONG timer()
{
  static struct timeval t;
  struct timeval a, b;

  GetSysTime(&a);
  b = a;
  SubTime(&b, &t);
  t = a;

  return b.tv_secs*1000 + b.tv_micro/1000;
}


int main()
{
  OpenDevice("timer.device", 0, &timereq, 0);
  TimerBase = timereq.io_Device;

  timer();

  for(int n = 200; n--;)
    printf("%i ", n);

  ULONG t = timer();

  printf("\n%u ms\n", t);

  CloseDevice(&timereq);
  return 0;
}
Hey Thanks!

I will try this code tomorrow.

Quote:
If this is your first taste of the easy-to-use AmigaOS APIs then you may throw up in the corner over there --->
LOL. It's certainly not Python or Java is it. I think when it comes to programming the Amiga you begin to realise how old the system is. I'm sure a lot of the architecture has been updated with OS4/MOS/AROS, but it still feels very antiquated.
 
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling CLib37 with VBCC 0.9 tnt23 Coders. General 2 29 July 2013 10:28
Old timer, new member ! cyberknight Member Introductions 14 23 May 2009 11:07
VBCC 0.8j for Windows hitchhikr Coders. General 11 09 October 2008 00:58
uaehf.device and HDToolbox: Error 224 reading device description Ebster support.WinUAE 3 16 September 2008 09:24
Kickstart 1.3 and GCC or VBCC? cdoty Coders. General 1 23 April 2005 06:10

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:12.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.08318 seconds with 13 queries