/[gxemul]/trunk/src/devices/dev_cons.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/devices/dev_cons.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 10 by dpavlin, Mon Oct 8 16:18:27 2007 UTC revision 42 by dpavlin, Mon Oct 8 16:22:32 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2003-2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2003-2007  Anders Gavare.  All rights reserved.
3   *   *
4   *  Redistribution and use in source and binary forms, with or without   *  Redistribution and use in source and binary forms, with or without
5   *  modification, are permitted provided that the following conditions are met:   *  modification, are permitted provided that the following conditions are met:
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *     *  
27   *   *
28   *  $Id: dev_cons.c,v 1.26 2005/06/26 11:43:48 debug Exp $   *  $Id: dev_cons.c,v 1.42 2007/06/15 18:44:19 debug Exp $
29   *     *  
30   *  A console device.  (Fake, only useful for simple tests.)   *  COMMENT: A simple console device, for the test machines
  *  It is hardwared to the lowest available MIPS hardware IRQ, and only  
  *  interrupts CPU 0.  
31   *   *
32   *  This device provides memory mapped I/O for a simple console supporting   *  This device provides memory mapped I/O for a simple console supporting
33   *  putchar (writing to memory) and getchar (reading from memory), and   *  putchar (writing to memory) and getchar (reading from memory), and
# Line 43  Line 41 
41    
42  #include "console.h"  #include "console.h"
43  #include "cpu.h"  #include "cpu.h"
44  #include "devices.h"  #include "device.h"
45  #include "machine.h"  #include "machine.h"
46  #include "memory.h"  #include "memory.h"
47  #include "misc.h"  #include "misc.h"
48    
49    #include "testmachine/dev_cons.h"
50    
51  #define CONS_TICK_SHIFT         14  #define CONS_TICK_SHIFT         14
52    
53  struct cons_data {  struct cons_data {
54          int             console_handle;          int                     console_handle;
55          int             irq_nr;          int                     in_use;
56            struct interrupt        irq;
57  };  };
58    
59    
60  /*  DEVICE_TICK(cons)
  *  dev_cons_tick():  
  */  
 void dev_cons_tick(struct cpu *cpu, void *extra)  
61  {  {
         struct cpu *c = cpu->machine->cpus[0];  
62          struct cons_data *d = extra;          struct cons_data *d = extra;
63    
         cpu_interrupt_ack(c, d->irq_nr);  
   
64          if (console_charavail(d->console_handle))          if (console_charavail(d->console_handle))
65                  cpu_interrupt(c, d->irq_nr);                  INTERRUPT_ASSERT(d->irq);
66            else
67                    INTERRUPT_DEASSERT(d->irq);
68  }  }
69    
70    
71  /*  DEVICE_ACCESS(cons)
  *  dev_cons_access():  
  */  
 int dev_cons_access(struct cpu *cpu, struct memory *mem,  
         uint64_t relative_addr, unsigned char *data, size_t len,  
         int writeflag, void *extra)  
72  {  {
73          struct cons_data *d = extra;          struct cons_data *d = extra;
74          int i;          unsigned int i;
75    
76          /*  Exit the emulator:  */          /*  Exit the emulator:  */
77          if (relative_addr == DEV_CONS_HALT) {          if (relative_addr == DEV_CONS_HALT) {
78                  cpu->running = 0;                  /*  cpu->running = 0;
79                  cpu->machine->exit_without_entering_debugger = 1;                      cpu->machine->exit_without_entering_debugger = 1;
80                  return 1;                      return 1;  */
81                    /*  TODO: this doesn't work yet. for now, let's
82                        simply use exit()  */
83                    exit(1);
84          }          }
85    
86          if (writeflag == MEM_WRITE) {          if (writeflag == MEM_WRITE) {
# Line 118  int dev_cons_access(struct cpu *cpu, str Line 112  int dev_cons_access(struct cpu *cpu, str
112  }  }
113    
114    
115  /*  DEVINIT(cons)
  *  dev_cons_init():  
  */  
 int dev_cons_init(struct machine *machine, struct memory *mem,  
         uint64_t baseaddr, char *name, int irq_nr)  
116  {  {
117          struct cons_data *d;          struct cons_data *d;
118          char *name2;          char *name3;
119          size_t nlen;          size_t nlen;
120    
121          d = malloc(sizeof(struct cons_data));          CHECK_ALLOCATION(d = malloc(sizeof(struct cons_data)));
         if (d == NULL) {  
                 fprintf(stderr, "out of memory\n");  
                 exit(1);  
         }  
122          memset(d, 0, sizeof(struct cons_data));          memset(d, 0, sizeof(struct cons_data));
         d->irq_nr = irq_nr;  
         d->console_handle = console_start_slave(machine, name);  
123    
124          nlen = strlen(name) + 20;          nlen = strlen(devinit->name) + 10;
125          name2 = malloc(nlen);          if (devinit->name2 != NULL)
126          if (name2 == NULL) {                  nlen += strlen(devinit->name2) + 10;
127                  fprintf(stderr, "out of memory in dev_cons_init()\n");          CHECK_ALLOCATION(name3 = malloc(nlen));
128                  exit(1);          if (devinit->name2 != NULL && devinit->name2[0])
129          }                  snprintf(name3, nlen, "%s [%s]", devinit->name, devinit->name2);
         if (name != NULL && name[0])  
                 snprintf(name2, nlen, "cons [%s]", name);  
130          else          else
131                  snprintf(name2, nlen, "cons");                  snprintf(name3, nlen, "%s", devinit->name);
132    
133            INTERRUPT_CONNECT(devinit->interrupt_path, d->irq);
134    
135          memory_device_register(mem, name2, baseaddr, DEV_CONS_LENGTH,          d->in_use = devinit->in_use;
136              dev_cons_access, d, MEM_DEFAULT, NULL);          d->console_handle = console_start_slave(devinit->machine, name3,
137          machine_add_tickfunction(machine, dev_cons_tick, d, CONS_TICK_SHIFT);              d->in_use);
138    
139            memory_device_register(devinit->machine->memory, name3,
140                devinit->addr, DEV_CONS_LENGTH, dev_cons_access, d,
141                DM_DEFAULT, NULL);
142            machine_add_tickfunction(devinit->machine, dev_cons_tick,
143                d, CONS_TICK_SHIFT);
144    
145          return d->console_handle;          /*  NOTE: Ugly cast into pointer  */
146            devinit->return_ptr = (void *)(size_t)d->console_handle;
147            return 1;
148  }  }
149    

Legend:
Removed from v.10  
changed lines
  Added in v.42

  ViewVC Help
Powered by ViewVC 1.1.26