tabgen.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Atmel Mega8 based LMD18245 controller
  3. * Lookup table generator
  4. *
  5. * Copyright (c) 2009-2020 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #undef M_PI
  21. #define M_PI 3.14159265358979323846264338327
  22. #define COMPILE_YEAR ((((__DATE__[7] - '0') * 10 + \
  23. (__DATE__[8] - '0')) * 10 + \
  24. (__DATE__[9] - '0')) * 10 + \
  25. (__DATE__[10] - '0'))
  26. #define COMPILE_MONTH ((__DATE__[2] == 'n' ? (__DATE__[1] == 'a' ? 0 : 5) \
  27. : __DATE__[2] == 'b' ? 1 \
  28. : __DATE__[2] == 'r' ? (__DATE__[0] == 'M' ? 2 : 3) \
  29. : __DATE__[2] == 'y' ? 4 \
  30. : __DATE__[2] == 'l' ? 6 \
  31. : __DATE__[2] == 'g' ? 7 \
  32. : __DATE__[2] == 'p' ? 8 \
  33. : __DATE__[2] == 't' ? 9 \
  34. : __DATE__[2] == 'v' ? 10 : 11) + 1)
  35. #define COMPILE_DAY ((__DATE__[4] == ' ' ? 0 : __DATE__[4] - '0') * 10 + \
  36. (__DATE__[5] - '0'))
  37. static int gen_lmd_tab(unsigned int nr_steps)
  38. {
  39. double pos;
  40. double s;
  41. unsigned int lmd1, lmd2;
  42. unsigned int count;
  43. unsigned char *buf;
  44. buf = malloc(nr_steps * 2);
  45. if (!buf) {
  46. fprintf(stderr, "Out of memory.\n");
  47. return 1;
  48. }
  49. /* Generate the lookup table. */
  50. /* sin() takes radians as arg. pi rad == 180deg */
  51. for (count = 0; count < nr_steps * 2; count++) {
  52. pos = (M_PI * (double)count) / ((double)nr_steps * 2.0);
  53. s = fabs(sin(pos)) * (double)0xF;
  54. lmd1 = (unsigned int)round(s) & 0xFu;
  55. s = fabs(cos(pos)) * (double)0xF;
  56. lmd2 = (unsigned int)round(s) & 0xFu;
  57. buf[count] = lmd1 | (lmd2 << 4);
  58. }
  59. /* Print the lookup table. */
  60. printf("steptable:\n");
  61. for (count = 0; count < nr_steps * 2; count++) {
  62. if (count % 8 == 0)
  63. printf("%s.db 0x%02X", count ? "\n" : "", buf[count]);
  64. else
  65. printf(", 0x%02X", buf[count]);
  66. }
  67. printf("\n\n");
  68. free(buf);
  69. return 0;
  70. }
  71. int main(int argc, char **argv)
  72. {
  73. unsigned long nr_steps;
  74. int err;
  75. if (argc != 2) {
  76. fprintf(stderr, "Usage: %s NR_OF_STEPS\n", argv[0]);
  77. return 1;
  78. }
  79. nr_steps = strtoul(argv[1], NULL, 10);
  80. if (nr_steps < 2 || nr_steps > 60) {
  81. fprintf(stderr, "Please select a step count between 2 and 60\n");
  82. return 1;
  83. }
  84. printf("; THIS FILE IS GENERATED. DO NOT EDIT.\n");
  85. printf("\n\n");
  86. printf(".equ NR_STEPS=%lu\n", nr_steps);
  87. printf("\n");
  88. printf(".cseg\n");
  89. err = gen_lmd_tab(nr_steps);
  90. if (err)
  91. return 1;
  92. printf("; For reference store the build parameters in the binary image\n");
  93. printf(".cseg\n");
  94. printf(".db 0,0,0,0\n");
  95. printf(".db \"NR_STEPS=%lu\"%s\n", nr_steps, ((nr_steps > 9) ? ",0" : ""));
  96. printf(".db 0,0,0,0\n");
  97. printf(".db \"BUILD_DATE=%04u.%02u.%02u\",0\n",
  98. (unsigned int)COMPILE_YEAR,
  99. (unsigned int)COMPILE_MONTH,
  100. (unsigned int)COMPILE_DAY);
  101. printf("\n");
  102. return 0;
  103. }