Advertisement
uaa

avrclock.c (WTFPL 2.0 license)

uaa
Jul 23rd, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define BUFSIZE 512
  5.  
  6. struct inst_clk {
  7. char *inst;
  8. char *avr_clk;
  9. char *lgt_clk;
  10. };
  11.  
  12. static struct inst_clk inst_entry[] = {
  13. /* inst AVR LGT8x */
  14. {"adiw", "2", "1"},
  15. {"sbiw", "2", "1"},
  16. {"mul", "2", "1"},
  17. {"muls", "2", "1"},
  18. {"mulsu", "2", "1"},
  19. {"fmul", "2", "1"},
  20. {"fmuls", "2", "1"},
  21. {"fmulsu", "2", "1"},
  22. {"rjmp", "2", "1"},
  23. {"ijmp", "2", "1"},
  24. {"jmp", "3", NULL},
  25. {"rcall", "3", "1"},
  26. {"icall", "3", "1"},
  27. {"call", "4", NULL},
  28. {"ret", "4", "2"},
  29. {"reti", "4", "2"},
  30. {"cpse", "1/2/3", "1/2"},
  31. {"sbrc", "1/2/3", "1/2"},
  32. {"sbrs", "1/2/3", "1/2"},
  33. {"sbic", "1/2/3", "1/2"},
  34. {"sbis", "1/2/3", "1/2"},
  35. {"brbs", "1/2", NULL},
  36. {"brbc", "1/2", NULL},
  37. {"breq", "1/2", NULL},
  38. {"brne", "1/2", NULL},
  39. {"brcs", "1/2", NULL},
  40. {"brcc", "1/2", NULL},
  41. {"brsh", "1/2", NULL},
  42. {"brlo", "1/2", NULL},
  43. {"brmi", "1/2", NULL},
  44. {"brpl", "1/2", NULL},
  45. {"brge", "1/2", NULL},
  46. {"brlt", "1/2", NULL},
  47. {"brhs", "1/2", NULL},
  48. {"brhc", "1/2", NULL},
  49. {"brts", "1/2", NULL},
  50. {"brtc", "1/2", NULL},
  51. {"brvs", "1/2", NULL},
  52. {"brvc", "1/2", NULL},
  53. {"brie", "1/2", NULL},
  54. {"brid", "1/2", NULL},
  55. {"sbi", "2", NULL},
  56. {"cbi", "2", NULL},
  57. {"ld", "2", "1"},
  58. {"ldd", "2", "1"},
  59. {"lds", "2", NULL},
  60. {"st", "2", "1"},
  61. {"std", "2", "1"},
  62. {"sts", "2", NULL},
  63. {"lpm", "3", "2"},
  64. {"push", "2", "1"},
  65. {"pop", "2", "1"},
  66. };
  67.  
  68. static char *get_clk(char *str, int mode)
  69. {
  70. int i;
  71.  
  72. for (i = 0; i < sizeof(inst_entry) / sizeof(struct inst_clk); i++) {
  73. if (!strcmp(str, inst_entry[i].inst)) {
  74. if (mode && inst_entry[i].lgt_clk != NULL)
  75. return inst_entry[i].lgt_clk;
  76. else
  77. return inst_entry[i].avr_clk;
  78. }
  79. }
  80.  
  81. return NULL;
  82. }
  83.  
  84. static char *find(char *str, int mode)
  85. {
  86. char *p, *l, *r;
  87. char buf[BUFSIZE];
  88.  
  89. strncpy(buf, str, sizeof(buf));
  90.  
  91. for ((p = strtok_r(buf, " \t", &l)); p;
  92. (p = strtok_r(NULL, " \t", &l))) {
  93. r = get_clk(p, mode);
  94. if (r != NULL)
  95. return r;
  96. }
  97.  
  98. return "1";
  99. }
  100.  
  101. int main(int argc, char *argv[])
  102. {
  103. char *p;
  104. char buf[BUFSIZE];
  105.  
  106. while (fgets(buf, sizeof(buf), stdin) != NULL) {
  107. p = strpbrk(buf, "\n\r");
  108. if (p != NULL)
  109. *p = '\0';
  110.  
  111. p = strchr(buf, ':');
  112. if (p == NULL || !strlen(p + 1))
  113. p = "";
  114. else
  115. p = find(p + 1, argc > 1);
  116.  
  117. printf("%-5s %s\n", p, buf);
  118. }
  119.  
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement