Advertisement
LightningStalker

picloops.c

May 27th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.21 KB | None | 0 0
  1. /* compile with "gcc -Wall -opicloops picloops.c -lm"
  2.  *
  3.  * picloops.c - calculate PIC microcontroller timing loops of 1-4 stages
  4.  * using given inner loop overhead
  5.  *
  6.  * Time in seconds and clock speed in MHz shall be specified on the
  7.  * command line in any order.  For instance
  8.  * $ ./picloops 4 60
  9.  *
  10.  * Loosely based on picasm's pic loop calculator
  11.  *
  12.  * Author: Robert Dvoracek
  13.  * Contact/Website: http://kickme.to/lightningstalker
  14.  *
  15.  * Bugs: slightly inaccurate for inner loop overhead values other than 9
  16.  *
  17.  * A great source of info can be found at
  18.  * http://www.piclist.com/techref/microchip/PIC16DelayTutorial.htm*/
  19.  
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include <errno.h>
  23. #include <stdlib.h>
  24.  
  25. /*#define   INNER_LOOP_OVERHEAD 4.0
  26. #define MACHINE_CYCLE_MAX (THREE_STAGE_MAX) * 256 + 6
  27. #define SINGLE_STAGE_MAX INNER_LOOP_OVERHEAD * 256 + 1
  28. #define TWO_STAGE_MAX (SINGLE_STAGE_MAX + 1) * 256 + 3
  29. #define THREE_STAGE_MAX (TWO_STAGE_MAX - 1) * 256 + 2
  30. #define FOUR_STAGE_MAX MACHINE_CYCLE_MAX
  31.  
  32. #define FACTORB (INNER_LOOP_OVERHEAD * 256 + 2)
  33. #define FACTORC (FACTORB * 256 + 2)
  34. #define FACTORD (FACTORC * 256 + 2)
  35.  
  36. #define TWO_STAGE_ADD (FACTORB - 5)
  37. #define THREE_STAGE_ADD (FACTORB + FACTORC - 9)
  38. #define FOUR_STAGE_ADD (FACTORB + FACTORC + FACTORD - 13)*/
  39.  
  40. int main(int argc, char **argv) {
  41.   int counterA, counterB, counterC, counterD, leftover;
  42.   double inner_loop_overhead, machine_cycle_max, single_stage_max,
  43.       two_stage_max, three_stage_max, four_stage_max;
  44.   double factorB, factorC, factorD, two_stage_add, three_stage_add,
  45.       four_stage_add;
  46.   double time, cycles;
  47.  
  48.   if (argc != 4) {
  49.       puts("Usage: $ picloops freqMHz time #cycles_in_inner_loop");
  50.       return(1);
  51.   }
  52.      
  53.   counterA = counterB = counterC = counterD = -1;
  54.   time = 4.0 / (atof(argv[2]) * 1000000.0);
  55.   cycles = floor(atof(argv[1]) / time);
  56.  
  57.   inner_loop_overhead = floor(atof(argv[3]));
  58.   single_stage_max = inner_loop_overhead * 256 + 1;
  59.   two_stage_max = (single_stage_max + 1) * 256 + 3;
  60.   three_stage_max = (two_stage_max - 1) * 256 + 2;
  61.   machine_cycle_max = (three_stage_max) * 256 + 6;
  62.   four_stage_max = machine_cycle_max;
  63.  
  64.   factorB = (inner_loop_overhead * 256 + 2);
  65.   factorC = (factorB * 256 + 2);
  66.   factorD = (factorC * 256 + 2);
  67.  
  68.   two_stage_add = (factorB - 5);
  69.   three_stage_add = (factorB + factorC - 9);
  70.   four_stage_add = (factorB + factorC + factorD - 13);
  71.  
  72.   if(cycles <= single_stage_max) {
  73.     counterA = ((cycles - 1.0) / inner_loop_overhead);
  74.     leftover = (cycles - (inner_loop_overhead * counterA) - 1);
  75.   }
  76.    
  77.   else if(cycles <= two_stage_max) {
  78.     counterB = ((cycles + two_stage_add) / factorB);
  79.     counterA = ((cycles + two_stage_add - factorB * counterB) / inner_loop_overhead);
  80.     leftover = (cycles - (inner_loop_overhead * counterA + factorB * counterB) + two_stage_add);
  81.   }
  82.   else if(cycles <= three_stage_max) {
  83.     counterC = ((cycles + three_stage_add) / factorC);
  84.     counterB = ((cycles + three_stage_add - counterC * factorC) / factorB);
  85.     counterA = ((cycles + three_stage_add - (counterC * factorC + counterB * factorB)) / inner_loop_overhead);
  86.     leftover = (cycles - (inner_loop_overhead * counterA + factorB * counterB + factorC * counterC) + three_stage_add);
  87.   }
  88.   else if(cycles <= four_stage_max) {
  89.     counterD = ((cycles + four_stage_add) / factorD);
  90.     counterC = ((cycles + four_stage_add - counterD * factorD) / factorC);
  91.     counterB = ((cycles + four_stage_add - (counterD * factorD + counterC * factorC)) / factorB);
  92.     counterA = ((cycles + four_stage_add - (counterD * factorD + counterC * factorC + counterB * factorB)) / inner_loop_overhead);
  93.     leftover = (cycles - (inner_loop_overhead * counterA + factorB * counterB + factorC * counterC + factorD * counterD) + four_stage_add);
  94.   }
  95.   if(counterA == 256)
  96.     counterA = 0;
  97.   if(counterB == 256)
  98.     counterB = 0;
  99.   if(counterC == 256)
  100.     counterC = 0;    
  101.  
  102.   printf("%d %d %d %d + %d\n", counterA, counterB, counterC, counterD, leftover);
  103.   printf("%f %f %f %f\n", two_stage_add, three_stage_add, four_stage_add, single_stage_max);
  104. return(0);
  105. }
  106. //xa - x + (x - 1)
  107. //9a - 1
  108. //9a + 2306b - 2301
  109. //9a + 2306b + 590338c - 592635
  110. //9a + 2306b + 590338c + 151126530d - 151719161
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement