Device Management

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Therx

Device Management

Post by Therx »

I'm writing my DM and have chosen to keep a 3D array of pointers for the device open, read, write, close, seek etc. functions. My code (below) goes to the read(...) function but causes an Invalid Opcode (0x06) exception before geting to the beginning of the function pointed to. Has anyone got any ideas why?

Thanks

Main DM File

Code: Select all

static volatile dev_t devices[NUM_TYPES][MAX_DEVS];
static volatile unsigned int totaldev[NUM_TYPES];

int add_dev(int type, dev_t *device)
{
   memcpy(&devices[type][totaldev[type]], &device, sizeof(dev_t));
   totaldev[type]++;
   return totaldev[type] - 1;
}

void read(int type, int id, int location, char *buffer)
{
   devices[type][id].p_read(location, buffer);
}
Header file

Code: Select all

typedef void (*ptr_t)(int location, char *buffer);

typedef struct
{
   ptr_t p_open, p_read, p_write, p_seek, p_close;
} dev_t;

#define NUM_TYPES      7
#define MAX_DEVS       10
#define FDD            0
#define HDD            1
#define NIC            2
#define SOUND          3
#define PRINT          4
#define VIDEO          5
#define CDD            6

int add_dev(int type, dev_t *device);
void read(int type, int id, int location, char *buffer);
Tim

Re:Device Management

Post by Tim »

Therx wrote:

Code: Select all

   memcpy(&devices[type][totaldev[type]], &device, sizeof(dev_t));
Shouldn't this line be:

Code: Select all

   memcpy(&devices[type][totaldev[type]], device, sizeof(dev_t));
device is already a pointer; &device is a pointer to a pointer, which is now what you want.
Therx

Re:Device Management

Post by Therx »

Thanks that made the difference. How can you spot such small mistakes? I'd have spent days trying find that :-[
Tim

Re:Device Management

Post by Tim »

Experience :). I've seen exactly the same bug (passing a pointer to a pointer to memcpy) a couple of times recently.
Post Reply