home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / rom / exec / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-03  |  9.9 KB  |  412 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: init.c,v 1.31 1997/02/03 18:35:33 digulla Exp $
  4.  
  5.     Desc: startup code for AROS (main())
  6.     Lang: english
  7. */
  8. #define AROS_ALMOST_COMPATIBLE
  9. #include <aros/system.h>
  10. #include <stdlib.h>
  11. #include <signal.h>
  12. #ifndef _AMIGA
  13. #define timeval     linux_timeval
  14. #include <sys/time.h>
  15. #undef timeval
  16. #else
  17. #include <sys/time.h>
  18. #endif
  19. #include <unistd.h>
  20. #include <stdio.h>
  21. #include <exec/execbase.h>
  22. #include <exec/memory.h>
  23. #include <exec/devices.h>
  24. #include <proto/aros.h>
  25. #include <proto/exec.h>
  26. #include <dos/dos.h>
  27. #include <dos/dosextens.h>
  28. #include <dos/dostags.h>
  29. #include <proto/dos.h>
  30. #include <utility/tagitem.h>
  31. #include <aros/rt.h>
  32. #include <aros/arosbase.h>
  33. #include <hardware/intbits.h>
  34. #include "memory.h"
  35. #include <aros/machine.h>
  36. #include <aros/asmcall.h>
  37. #undef kprintf
  38.  
  39. extern void *ExecFunctions[];
  40. extern const struct Resident Utility_resident;
  41. extern const struct Resident Dos_resident;
  42. extern const struct Resident Graphics_resident;
  43. extern const struct Resident Intuition_resident;
  44. extern const struct Resident emul_handler_resident;
  45. extern const struct Resident Console_resident;
  46. extern void InitCore(void);
  47.  
  48. #define MEMSIZE 1024*1024*3
  49. /*#define STATIC_MEMORY */ /* So that gdb can disassemble it */
  50. static struct MemHeader mh;
  51. #ifdef STATIC_MEMORY
  52. static UBYTE memory[MEMSIZE+MEMCHUNK_TOTAL];
  53. #else
  54. UBYTE * memory;
  55. #endif
  56.  
  57. #define NUMVECT 131
  58.  
  59. struct ExecBase *SysBase;
  60. struct DosLibrary *DOSBase;
  61.  
  62. static int returncode=20;
  63. static struct AROSBase AROSBase;
  64.  
  65. extern struct Task * inputDevice;
  66. extern void debugmem (void);
  67.  
  68. static void idleTask (void)
  69. {
  70.     while (1)
  71.     {
  72.     if (inputDevice)
  73.         Signal (inputDevice, SIGBREAKF_CTRL_F);
  74.     }
  75. }
  76.  
  77. int submain(int argc,char *argv[]);
  78.  
  79. static int gargc;
  80. static char **gargv;
  81.  
  82. static void boot(void)
  83. {
  84.     returncode=submain(gargc,gargv);
  85.     RemTask(NULL);
  86. }
  87.  
  88. AROS_UFH2 (void, IntServer,
  89.     AROS_UFHA (struct List      *,intList,A1),
  90.     AROS_UFHA (struct ExecBase  *,SysBase,A6)
  91. )
  92. {
  93.     struct Interrupt * irq;
  94.  
  95.     ForeachNode (intList, irq)
  96.     {
  97.     if (AROS_UFC2 (int, irq->is_Code,
  98.         AROS_UFCA (APTR,              irq->is_Data, A1),
  99.         AROS_UFCA (struct ExecBase *, SysBase,      A6)
  100.     ))
  101.         break;
  102.     }
  103. }
  104.  
  105. AROS_UFH2(int, Dispatcher,
  106.     AROS_UFHA (APTR,              is_Data, A1),
  107.     AROS_UFHA (struct ExecBase *, SysBase, A6)
  108. )
  109. {
  110.     Disable();
  111.     /* Check if a task switch is necessary */
  112.     if(SysBase->TaskReady.lh_Head->ln_Succ!=NULL&&
  113.        SysBase->ThisTask->tc_Node.ln_Pri<=
  114.     ((struct Task *)SysBase->TaskReady.lh_Head)->tc_Node.ln_Pri)
  115.     {
  116.     /* Check if it is possible */
  117.     if(SysBase->TDNestCnt<0)
  118.     {
  119.         if(SysBase->ThisTask->tc_State==TS_RUN)
  120.         {
  121.         SysBase->ThisTask->tc_State=TS_READY;
  122.         Enqueue(&SysBase->TaskReady,&SysBase->ThisTask->tc_Node);
  123.         SysBase->AttnResched|=0x8000;
  124.         }
  125.     }else
  126.         SysBase->AttnResched|=0x80;
  127.     }
  128.     Enable();
  129.     /* Wasn't explicitly for me */
  130.     return 0;
  131. }
  132.  
  133. static APTR allocmem(ULONG size)
  134. {
  135.     UBYTE *ret;
  136.  
  137.     size = (size + MEMCHUNK_TOTAL-1) & ~(MEMCHUNK_TOTAL-1);
  138.  
  139.     ret = (UBYTE *)mh.mh_First;
  140.  
  141.     mh.mh_First = (struct MemChunk *)(ret + size);
  142.     mh.mh_First->mc_Next = NULL;
  143.     mh.mh_Free =
  144.     mh.mh_First->mc_Bytes = ((struct MemChunk *)ret)->mc_Bytes - size;
  145.  
  146.     return ret;
  147. }
  148.  
  149. void _aros_not_implemented (void)
  150. {
  151.     fprintf (stderr, "This function is not implemented\n");
  152. }
  153.  
  154. void aros_print_not_implemented (char * name)
  155. {
  156.     fprintf (stderr, "The function %s is not implemented\n", name);
  157. }
  158.  
  159. int main(int argc,char *argv[])
  160. {
  161.     ULONG * space;
  162.  
  163.     /* Put arguments into globals */
  164.     gargc=argc;
  165.     gargv=argv;
  166.  
  167.     /* Leave a space of 4096 bytes before the memory */
  168.     space = malloc (4096);
  169. #ifndef STATIC_MEMORY
  170.     memory = malloc (MEMSIZE+MEMCHUNK_TOTAL);
  171. #endif
  172.  
  173.     { /* erase space */
  174.     int size = 4096/sizeof(ULONG);
  175.  
  176.     while (--size)
  177.         *space ++ = 0xDEADBEEF;
  178.     }
  179.  
  180.     /*
  181.     Prepare first MemHeader. I cannot use exec functions
  182.     here because exec is not yet up.
  183.     */
  184.     mh.mh_Node.ln_Name = "chip memory"; /* Amiga has always chip, but maybe no fast */
  185.     mh.mh_Node.ln_Pri  = 0;
  186.     mh.mh_Attributes   = MEMF_CHIP|MEMF_PUBLIC; /* Public to my emulation */
  187.     mh.mh_First = (struct MemChunk *)
  188.         (((IPTR)memory + MEMCHUNK_TOTAL-1) & ~(MEMCHUNK_TOTAL-1));
  189.     mh.mh_First->mc_Next  = NULL;
  190.     mh.mh_First->mc_Bytes = MEMSIZE;
  191.  
  192.     mh.mh_Lower = mh.mh_First;
  193.     mh.mh_Upper = (UBYTE *)mh.mh_Lower+MEMSIZE;
  194.     mh.mh_Free    = MEMSIZE;
  195.  
  196.     /* The following allocations cannot and must not fail. */
  197.     {
  198.     /* Prepare exec.library */
  199.     ULONG neg, i;
  200.  
  201.     neg = AROS_ALIGN(LIB_VECTSIZE*NUMVECT);
  202.  
  203.     SysBase = (struct ExecBase *)
  204.         ((UBYTE *)allocmem (neg + sizeof (struct ExecBase)) + neg);
  205.  
  206.     for (i=1; i<=NUMVECT; i++)
  207.     {
  208.         __AROS_INITVEC (SysBase, i);
  209.         __AROS_SETVECADDR (SysBase, i, ExecFunctions[i-1]);
  210.     }
  211.  
  212.     SysBase->LibNode.lib_Node.ln_Name = "exec.library";
  213.     SysBase->LibNode.lib_Version      = 41;
  214.     SysBase->LibNode.lib_Revision      = 10;
  215.  
  216.     SysBase->DebugData = &AROSBase;
  217.  
  218.     AROSBase.kprintf = (void *)kprintf;
  219.  
  220.     NEWLIST (&SysBase->MemList);
  221.     AddHead (&SysBase->MemList, &mh.mh_Node);
  222.     NEWLIST (&SysBase->ResourceList);
  223.     NEWLIST (&SysBase->DeviceList);
  224.     NEWLIST (&SysBase->IntrList);
  225.     NEWLIST (&SysBase->LibList);
  226.     NEWLIST (&SysBase->PortList);
  227.     NEWLIST (&SysBase->TaskReady);
  228.     NEWLIST (&SysBase->TaskWait);
  229.  
  230.     for (i=0; i<5; i++)
  231.     {
  232.         NEWLIST (&SysBase->SoftInts[i].sh_List);
  233.     }
  234.  
  235.     for (i=0; i<16; i++)
  236.     {
  237.         SysBase->IntVects[i].iv_Code = NULL;
  238.         SysBase->IntVects[i].iv_Data = NULL;
  239.         SysBase->IntVects[i].iv_Node = NULL;
  240.     }
  241.  
  242.     NEWLIST (&SysBase->SemaphoreList);
  243.  
  244.     /* There are no memhandlers yet.
  245.      * (not even the library flushing one which is part of ram/dos.library) */
  246.     NEWLIST ((struct List *)&SysBase->ex_MemHandlers);
  247.  
  248.     SysBase->IDNestCnt = 0;
  249.     SysBase->TDNestCnt = 0;
  250.     SysBase->AttnResched = 0;
  251.     }
  252.  
  253.     {
  254.     /* Add boot task */
  255.     struct Task    * t;
  256.     struct MemList * ml;
  257.  
  258.     ml=(struct MemList *)AllocMem(sizeof(struct MemList),MEMF_PUBLIC|MEMF_CLEAR);
  259.     t =(struct Task *)   AllocMem(sizeof(struct Task),   MEMF_PUBLIC|MEMF_CLEAR);
  260.     ml->ml_NumEntries     =1;
  261.     ml->ml_ME[0].me_Addr  =t;
  262.     ml->ml_ME[0].me_Length=sizeof(struct Task);
  263.  
  264.     NEWLIST(&t->tc_MemEntry);
  265.     AddHead(&t->tc_MemEntry,&ml->ml_Node);
  266.     t->tc_Node.ln_Name="Boot task";
  267.     t->tc_Node.ln_Pri=0;
  268.     t->tc_State=TS_RUN;
  269.     t->tc_SigAlloc=0xffff;
  270.     t->tc_SPLower = NULL;        /* This is the system's stack */
  271.     t->tc_SPUpper = (APTR)~0UL; /* all available addresses are ok */
  272.     SysBase->ThisTask=t;
  273.     }
  274.     {
  275.     /* Add idle task */
  276.     struct Task *t;
  277.     struct MemList *ml;
  278.     UBYTE *s;
  279.  
  280.     /* Allocate one header (incl. the first entry) and one additional
  281.         entry */
  282.     ml=(struct MemList *)AllocMem(sizeof(struct MemList)+sizeof(struct MemEntry),
  283.                       MEMF_PUBLIC|MEMF_CLEAR);
  284.     t =(struct Task *)   AllocMem(sizeof(struct Task),    MEMF_PUBLIC|MEMF_CLEAR);
  285.     s =(UBYTE *)         AllocMem(AROS_STACKSIZE,         MEMF_PUBLIC|MEMF_CLEAR);
  286.     ml->ml_NumEntries     =2;
  287.     ml->ml_ME[0].me_Addr  =t;
  288.     ml->ml_ME[0].me_Length=sizeof(struct Task);
  289.     ml->ml_ME[1].me_Addr  =s;
  290.     ml->ml_ME[1].me_Length=AROS_STACKSIZE;
  291.  
  292.     NEWLIST(&t->tc_MemEntry);
  293.     AddHead(&t->tc_MemEntry,&ml->ml_Node);
  294.     t->tc_SPLower=s;
  295.     t->tc_SPUpper=s+AROS_STACKSIZE;
  296.     t->tc_Node.ln_Name="Idle task";
  297.     t->tc_Node.ln_Pri=-128;
  298.     AddTask(t,&idleTask,NULL);
  299.     }
  300.     /* Install all interrupt servers */
  301.     {
  302.     int i;
  303.     for(i=0;i<16;i++)
  304.         if((1<<i)&(INTF_PORTS|INTF_COPER|INTF_VERTB|INTF_EXTER|INTF_SETCLR))
  305.         {
  306.         struct Interrupt *is;
  307.         struct SoftIntList *sil;
  308.         is=AllocMem(sizeof(struct Interrupt),MEMF_PUBLIC);
  309.         sil=AllocMem(sizeof(struct SoftIntList),MEMF_PUBLIC);
  310.         is->is_Code=&IntServer;
  311.         is->is_Data=sil;
  312.         NEWLIST((struct List *)sil);
  313.         SetIntVector(i,is);
  314.         }
  315.     }
  316.     InitCore();
  317.     /* Install the Dispatcher */
  318.     {
  319.     struct Interrupt *is;
  320.     is=(struct Interrupt *)AllocMem(sizeof(struct Interrupt),MEMF_PUBLIC);
  321.     is->is_Code=(void (*)())&Dispatcher;
  322.     AddIntServer(INTB_VERTB,is);
  323.     }
  324.     Enable();
  325.     Permit();
  326.  
  327.     debugmem ();
  328.  
  329.     (void) InitResident((struct Resident *)&Utility_resident,0);
  330.  
  331.     DOSBase = (struct DosLibrary *)InitResident((struct Resident *)&Dos_resident,0);
  332.  
  333.     (void) InitResident((struct Resident *)&Graphics_resident,0);
  334.     (void) InitResident((struct Resident *)&Intuition_resident,0);
  335.  
  336.     {
  337.     struct consolebase
  338.     {
  339.         struct Device device;
  340.     };
  341.  
  342.     struct consolebase *conbase;
  343.  
  344.     conbase=(struct consolebase *)InitResident((struct Resident *)&Console_resident,0);
  345.     /* AddDevice (&conbase->device); */
  346.     }
  347.  
  348.     DOSBase = (struct DosLibrary *)OpenLibrary (DOSNAME, 39);
  349.  
  350.     if (!DOSBase)
  351.     {
  352.     fprintf (stderr, "Cannot open dos.library");
  353.     exit (10);
  354.     }
  355.  
  356.     {
  357.     struct emulbase
  358.     {
  359.         struct Device eb_device;
  360.         struct Unit *eb_stdin;
  361.         struct Unit *eb_stdout;
  362.         struct Unit *eb_stderr;
  363.     };
  364.  
  365.     struct emulbase *emulbase;
  366.  
  367.     struct TagItem fhtags[]=
  368.     { { TAG_END, 0 } };
  369.  
  370.     struct FileHandle *fh_stdin=(struct FileHandle *)AllocDosObject(DOS_FILEHANDLE,fhtags);
  371.     struct FileHandle *fh_stdout=(struct FileHandle *)AllocDosObject(DOS_FILEHANDLE,fhtags);
  372.  
  373.     struct TagItem bootprocess[]=
  374.     {
  375.         { NP_Entry,     (IPTR)boot },
  376.         { NP_Input,     (IPTR)MKBADDR(fh_stdin) },
  377.         { NP_Output,    (IPTR)MKBADDR(fh_stdout) },
  378.         { NP_Name,        (IPTR)"Boot process" },
  379.         { NP_Cli,        1 },
  380.         { TAG_END, }
  381.     };
  382.  
  383.     emulbase = (struct emulbase *) InitResident (
  384.         (struct Resident *)&emul_handler_resident,
  385.         0
  386.     );
  387.     /* AddDevice (&emulbase->eb_device); */
  388.  
  389.     AssignLock ("C",    Lock ("SYS:c",    SHARED_LOCK));
  390.     AssignLock ("S",    Lock ("SYS:s",    SHARED_LOCK));
  391.     AssignLock ("Libs", Lock ("SYS:libs", SHARED_LOCK));
  392.     AssignLock ("Devs", Lock ("SYS:devs", SHARED_LOCK));
  393.  
  394.     fh_stdin->fh_Device  =&emulbase->eb_device;
  395.     fh_stdin->fh_Unit    =emulbase->eb_stdin;
  396.     fh_stdout->fh_Device =&emulbase->eb_device;
  397.     fh_stdout->fh_Unit   =emulbase->eb_stdout;
  398.  
  399.     /* AROSBase.StdOut = MKBADDR(fh_stdout); */
  400.     AROSBase.StdOut = stderr;
  401.  
  402.     CreateNewProc (bootprocess);
  403.     }
  404.  
  405.     RemTask(NULL); /* get rid of Boot task */
  406.  
  407.     Switch (); /* Rescedule */
  408.  
  409.     /* Should never get here... */
  410.     return 21;
  411. }
  412.