Guest User

an old multicore demo for plain dos

a guest
May 27th, 2026
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.98 KB | Source Code | 0 0
  1. // bareBones DOS multi core setup original by bloodwych around 2013 Foldername
  2. // Beware that the worker core operates in a bare metal Ring 0 flat mode
  3. // License: GPL3+
  4. // build ok 2026 using djgpp gcc 2core_demo.c apboot.s -o 2coreD.exe -s
  5.  
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <dpmi.h>
  10. #include <dos.h>
  11. #include <sys/farptr.h>
  12. #include <go32.h>
  13. #include <pc.h>
  14. #include <conio.h>
  15.  
  16. extern uint8_t apStart[];
  17. extern uint8_t apEnd[];
  18. extern uint8_t ap32[];
  19. extern uint8_t gdt[];
  20. extern uint8_t patchJmp[];
  21. extern uint8_t patchGdt[];
  22. extern uint8_t patchStack[];
  23. extern uint8_t patchMain[];
  24.  
  25. /*
  26. Entry Point fuer den Application Processor
  27. */
  28. void apMain() {
  29.     uint16_t *vga;
  30.        uint32_t delay;  
  31.     uint32_t tick;
  32.     uint32_t ecgPos;
  33.     uint16_t traceChar;
  34.     uint32_t baseOfs;
  35.     uint32_t upSecs;
  36.     uint32_t upMins;
  37.     uint32_t tscLow;
  38.     uint32_t tscHigh;
  39.         uint64_t tscNow;
  40.     uint64_t tscOld;
  41.     uint32_t cpuHz;
  42.     volatile uint8_t *state;
  43.     volatile uint8_t *alive;
  44.  
  45.     // paging is noch off also direct memory map auf vga buffer  
  46.     vga = (uint16_t *)0xB8000;
  47.     tick = 0;
  48.         ecgPos = 2;  
  49.     baseOfs = 40;
  50.     upSecs = 0;
  51.     upMins = 0;
  52.    
  53.        cpuHz = 2000000000;
  54.     state = (volatile uint8_t *)0x90100;
  55.     alive = (volatile uint8_t *)0x90101;
  56.     cpuHz = *((volatile uint32_t *)0x90104); // bogomips
  57.  
  58.     *alive = 1;
  59.      for(delay = 2; delay < 22; delay++) {
  60.         vga[baseOfs + delay] = 0x0A00 | '_';
  61.      }  
  62.  
  63.     // offset fur den delta verglech im loop festlegen
  64.     __asm__ __volatile__("rdtsc" : "=a"(tscLow), "=d"(tscHigh));
  65.     tscOld = ((uint64_t)tscHigh << 32) | tscLow;
  66.    
  67.     while(*state != 0xFF) {
  68.         // rdtsc lesen blockt background interrupt noize aus
  69.         __asm__ __volatile__("rdtsc" : "=a"(tscLow), "=d"(tscHigh));
  70.         tscNow = ((uint64_t)tscHigh << 32) | tscLow;
  71.        
  72.         // target frame pacing auf 25 hz
  73.         if (tscNow - tscOld >= (cpuHz / 25)) {
  74.             tscOld = tscNow;
  75.            
  76.             // ipc signal ueberpruefen
  77.             if (*state == 1) {
  78.                 // trigger herz phasen zyklisch
  79.                 if(tick == 0) {
  80.                     vga[baseOfs + 1] = 0x0E03;
  81.                     // herz zeichnen
  82.                     traceChar = 0x0A00 | 'X';
  83.                 } else if(tick == 2) {
  84.                     vga[baseOfs + 1] = 0x0604;
  85.                    
  86.                     traceChar = 0x0A00 | '_';
  87.                 } else if(tick == 6) {
  88.                     vga[baseOfs + 1] = 0x0720;
  89.                     // draw clear phase erzeugt pump efekt
  90.                     traceChar = 0x0A00 | '_';
  91.                 } else {
  92.                     traceChar = 0x0A00 | '_';
  93.                 }
  94.  
  95.                 // sweep visualisieren
  96.                 vga[baseOfs + ecgPos] = traceChar;
  97.                
  98.                 // abstand zum crt beam erzeugen
  99.                 if(ecgPos < 21) {
  100.                     vga[baseOfs + ecgPos + 1] = 0x0720;
  101.                 } else {
  102.                     vga[baseOfs + 2] = 0x0720;
  103.                 }
  104.  
  105.                 ecgPos++;
  106.                 if(ecgPos > 21) {
  107.                     ecgPos = 2;
  108.                 }
  109.             } else {
  110.                 // loesche grafik sweep
  111.                 for(delay = 1; delay < 22; delay++) {
  112.                     vga[baseOfs + delay] = 0x0720;
  113.                 }
  114.  
  115.                 // springe an position 72 direkt for die uhr
  116.                 if(tick == 0) {
  117.                     vga[72] = 0x0E03;
  118.                 } else if(tick == 2) {
  119.                     vga[72] = 0x0604;
  120.                 } else if(tick == 6) {
  121.                     vga[72] = 0x0720;
  122.                 }
  123.             }
  124.  
  125.             tick++;
  126.            
  127.             if(tick > 24) {
  128.                 tick = 0;
  129.                
  130.                 upSecs++;
  131.                 if(upSecs > 59) {
  132.                     upSecs = 0;
  133.                     upMins++;
  134.                 }
  135.                
  136.                 vga[74] = 0x0A00 | (48 + (upMins / 10));
  137.                 vga[75] = 0x0A00 | (48 + (upMins % 10));
  138.                 vga[76] = 0x0A00 | ' ';
  139.                 vga[77] = 0x0A00 | (48 + (upSecs / 10));
  140.                 vga[78] = 0x0A00 | (48 + (upSecs % 10));
  141.                 vga[79] = 0x0a00;
  142.             }
  143.         }
  144.  
  145.         // smt thread pause hint
  146.         __asm__ __volatile__("pause");
  147.     }
  148.  
  149.     // CPU anhalten
  150.     __asm__ __volatile__(
  151.         "cli\n\t"
  152.         ".Lap_halt: hlt\n\t"
  153.         "jmp .Lap_halt"
  154.     );
  155.  
  156.     // Endemarker wird genutzt um die groesse der funktion zu berechnen
  157.     __asm__ __volatile__("jmp 1f \n\t .long 0xABBABEAF \n\t 1:");
  158. }
  159.  
  160.  
  161. /*
  162. Baut das Trampolin im Low Memory auf
  163. */
  164. void prepTram(uint32_t alignPhys) {
  165.     uint32_t sizePayld;
  166.     uint32_t ofsJmp, ofsGdt, ofs32, ofsTabl, ofsStack, ofsMain, ofsCFunc;
  167.     uint32_t stackTop;
  168.     uint8_t *scanPointer;
  169.     uint32_t funcSize;
  170.  
  171.     sizePayld = (uint32_t)(apEnd - apStart);
  172.     ofsJmp = (uint32_t)(patchJmp - apStart);
  173.     ofsGdt = (uint32_t)(patchGdt - apStart);
  174.     ofs32 = (uint32_t)(ap32 - apStart);
  175.     ofsTabl = (uint32_t)(gdt - apStart);
  176.     ofsStack = (uint32_t)(patchStack - apStart);
  177.     ofsMain = (uint32_t)(patchMain - apStart);
  178.     ofsCFunc = 512;
  179.         stackTop = alignPhys + 0xf000;  
  180.  
  181.     scanPointer = (uint8_t *)&apMain;
  182.     funcSize = 0;
  183.  
  184.     // durchsuche memory byteweise weill das memory alignment varieren kan
  185.     while(*(uint32_t*)scanPointer != 0xABBABEAF) {
  186.         funcSize++;
  187.         scanPointer++;
  188.     }
  189.    
  190.     // marker selbst inken
  191.     funcSize += 4;
  192.    
  193.     // printf("DEBUG prepTram payload scan done at %d bytes \n", funcSize);
  194.  
  195.     // payload muss exact an die page boundary da die CPU im real mode startet
  196.     _dosmemputb(apStart, sizePayld, alignPhys);
  197.    
  198.     _dosmemputb((void*)&apMain, funcSize, alignPhys + ofsCFunc);
  199.    
  200.     // Assembly code braucht absolute addressen weil relocations im real mode nicht greifen
  201.     _farpokel(_dos_ds, alignPhys + ofsJmp, alignPhys + ofs32);
  202.     _farpokel(_dos_ds, alignPhys + ofsGdt, alignPhys + ofsTabl);
  203.     _farpokel(_dos_ds, alignPhys + ofsStack, stackTop);
  204.     _farpokel(_dos_ds, alignPhys + ofsMain, alignPhys + ofsCFunc);
  205. }
  206.  
  207.  
  208. /*
  209. Mappe physikalischen Speicher in DPMI
  210. */
  211. int mapMem(uint32_t physAddr) {
  212.     __dpmi_meminfo memInfo;
  213.     int apicSel;
  214.    
  215.     memInfo.address = physAddr;
  216.     memInfo.size = 4096;
  217.    
  218.     // APIC mmio bereich
  219.     if (__dpmi_physical_address_mapping(&memInfo) != 0) {
  220.         return -1;
  221.     }
  222.    
  223.         apicSel = __dpmi_allocate_ldt_descriptors(1) ;  
  224.     __dpmi_set_segment_base_address(apicSel, memInfo.address);
  225.     __dpmi_set_segment_limit(apicSel, 4095);
  226.    
  227.     return apicSel;
  228. }
  229.  
  230. /*
  231. Wartet Mikrosekunden ueber IO Port
  232. */
  233. void microWait(uint32_t us) {
  234.     uint32_t i;
  235.     for (i = 0; i < us; i++) {
  236.         // dummy port read erzwingt delay auf dem ISA bus
  237.         inportb(0x80);
  238.     }
  239. }
  240.  
  241. /*
  242. Wartet Millisekunden
  243. */
  244. void milliWait(uint32_t ms) {
  245.     uint32_t i;
  246.     for (i = 0; i < ms; i++) {
  247.         microWait(1000);
  248.     }
  249. }
  250.  
  251. /*
  252. Sendet die IPI Hardware Sequenz an den Core
  253. */
  254. void startAp(int apicSel, uint8_t trampPage) {
  255.     uint32_t icrLow;
  256.  
  257.     printf("DEBUG startAp initiiere hardware sequenz \n");
  258.     fflush(stdout);
  259.  
  260.     // APIC fehler state leeren
  261.     _farpokel(apicSel, 0x0F0, 0x1FF);
  262.    
  263.     // INIT
  264.     icrLow = 0x000C4500;
  265.     _farpokel(apicSel, 0x300, icrLow);
  266.  
  267.     milliWait(10);
  268.    
  269.     // Der vector teil im SIPI zwingt den instruction pointer vom worker core auf unsere trampoline page
  270.     icrLow = 0x000C4600 | trampPage;
  271.     _farpokel(apicSel, 0x300, icrLow);
  272.  
  273.     milliWait(1);
  274.    
  275.     // spec sagt redundant senden da der APIC bus beim ersten packet noch busy sein koennte
  276.     _farpokel(apicSel, 0x300, icrLow);
  277.    
  278.     milliWait(10);
  279.    
  280.  
  281. }
  282.  
  283. /*
  284. CPU Takt grob ueber PIT + TSC messen
  285. */
  286. uint32_t estimateCpuHz() {
  287.  
  288.     uint32_t tscStart;
  289.     uint32_t tscEnd;
  290.     uint32_t cpuHz;
  291.  
  292.     outportb(0x43, 0x30);
  293.     outportb(0x40, 0xFF);
  294.     outportb(0x40, 0xFF);
  295.  
  296.     __asm__ __volatile__(
  297.         "rdtsc"
  298.         : "=a"(tscStart)
  299.         :
  300.         : "edx"
  301.     );
  302.  
  303.     while(((uint16_t)inportb(0x40) |
  304.           ((uint16_t)inportb(0x40) << 8)) > 0x1000) {
  305.  
  306.         __asm__ __volatile__("pause");
  307.     }
  308.  
  309.     __asm__ __volatile__(
  310.         "rdtsc"
  311.         : "=a"(tscEnd)
  312.         :
  313.         : "edx"
  314.     );
  315.  
  316.     cpuHz = (tscEnd - tscStart) * 18;
  317.  
  318.     if(cpuHz < 1000000) {
  319.         cpuHz = 100000000;
  320.     }
  321.  
  322.     return cpuHz;
  323. }
  324.  
  325. int main() {
  326.  
  327.     uint32_t physAddr;
  328.     int apicSel;
  329.     uint32_t alignPhys;
  330.     uint8_t trampPage;
  331.  
  332.     uint32_t tick;
  333.     uint32_t ecgPos;
  334.     char traceChar;
  335.     int waitMs;
  336.  
  337.     uint32_t cpuHz;
  338.  
  339.     physAddr = 0xFEE00000;
  340.  
  341.     printf("DEBUG main starte \n");
  342.     fflush(stdout);
  343.  
  344.      alignPhys = 0x90000;
  345.     trampPage = alignPhys / 4096;
  346.  
  347.     apicSel = mapMem(physAddr);
  348.  
  349.     prepTram(alignPhys);
  350.  
  351.     // CPU speed schaetzeisen rechne mit overhead
  352.     cpuHz = estimateCpuHz()*1.3;
  353.  
  354.     printf("DEBUG CPU Speed geschaetzt: %lu Hz\n", cpuHz);
  355.     fflush(stdout);
  356.  
  357.     // shared memory setup
  358.     _farpokeb(_dos_ds, alignPhys + 0x100, 1);
  359.     _farpokeb(_dos_ds, alignPhys + 0x101, 0);
  360.  
  361.     // cpu speed fuer AP core
  362.     _farpokel(_dos_ds, alignPhys + 0x104, cpuHz);
  363.  
  364.     // printf("DEBUG tramp page is %d \n", trampPage);
  365.     startAp(apicSel, trampPage);
  366.  
  367.     waitMs = 0;
  368.  
  369.     while(_farpeekb(_dos_ds, alignPhys + 0x101) == 0 &&
  370.           waitMs < 500) {
  371.  
  372.         delay(10);
  373.         waitMs += 10;
  374.     }
  375.  
  376.     if(_farpeekb(_dos_ds, alignPhys + 0x101) == 0) {
  377.  
  378.         printf("FEHLER Core 2 meldet sich nicht \n");
  379.         return -1;
  380.     }
  381.  
  382.     printf("DEBUG main AP Boot erfolgreich durchlaufen \n");
  383.     printf("Press any key to exit cleanly Core 2 will stay alive \n");
  384.     fflush(stdout);
  385.  
  386.     tick = 0;
  387.    ecgPos = 2;  
  388.  
  389.     textcolor(10);
  390.  
  391.     for(int i = 2; i < 22; i++) {
  392.  
  393.         gotoxy(i + 1, 1);
  394.         putch('_');
  395.     }
  396.  
  397.     while(!kbhit()) {
  398.  
  399.         if(tick == 0 || tick == 1) {
  400.  
  401.             gotoxy(2, 1);
  402.             textcolor(14);
  403.             putch(3);
  404.  
  405.             if(tick == 0) {
  406.                 traceChar = 'X';
  407.             } else {
  408.                 traceChar = '_';
  409.             }
  410.  
  411.         } else if(tick == 2 || tick == 3) {
  412.  
  413.             gotoxy(2, 1);
  414.             textcolor(6);
  415.             putch(4);
  416.             traceChar = '_';
  417.  
  418.         } else if(tick == 6) {
  419.  
  420.             gotoxy(2, 1);
  421.             textcolor(7);
  422.             putch(' ');
  423.             traceChar = '_';
  424.  
  425.         } else {
  426.  
  427.             traceChar = '_';
  428.         }
  429.  
  430.         if(ecgPos < 21) {
  431.  
  432.             gotoxy(ecgPos + 2, 1);
  433.             putch(' ');
  434.  
  435.         } else {
  436.  
  437.             gotoxy(3, 1);
  438.             putch(' ');
  439.         }
  440.  
  441.         gotoxy(ecgPos + 1, 1);
  442.  
  443.         textcolor(10);
  444.         putch(traceChar);
  445.  
  446.         ecgPos++;
  447.  
  448.         if(ecgPos > 21) {
  449.             ecgPos = 2;
  450.         }
  451.  
  452.         tick++;
  453.  
  454.         if(tick > 25) {
  455.             tick = 0;
  456.         }
  457.  
  458.         delay(40);
  459.     }
  460.  
  461.     // Uptime Mode aktivieren
  462.     _farpokeb(_dos_ds, alignPhys + 0x100, 0x0);
  463.  
  464.  
  465.     return 0;
  466. }
Advertisement
Add Comment
Please, Sign In to add comment