Add-in development tools for fx-9860G and fx-CG 50, to use with GCC and gint.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.c 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <string.h>
  2. #include <endianness.h>
  3. #include <fxg1a.h>
  4. /*
  5. ** Public API
  6. */
  7. /* checksum(): Sum of 8 big-endian shorts at 0x300 */
  8. uint16_t checksum(struct g1a const *g1a, size_t size)
  9. {
  10. uint16_t shorts[16] = { 0 };
  11. /* Extract 16 bytes from the file (maybe less are available) */
  12. int available = size - 0x300;
  13. if(available < 0) available = 0;
  14. if(available > 16) available = 16;
  15. memcpy(shorts, g1a->code + 0x100, available);
  16. /* Do the big-endian sum */
  17. uint16_t sum = 0;
  18. for(int i = 0; i < 8; i++) sum += htobe16(shorts[i]);
  19. return sum;
  20. }
  21. /* default_output(): Calculate default output file name */
  22. void default_output(const char *name, const char *suffix, char *output)
  23. {
  24. /* Check if there is a dot at the end of @name, before the last '/'.
  25. The dot must also not be in first position (hidden files) */
  26. size_t end = strlen(name) - 1;
  27. while(end >= 1 && name[end] != '/' && name[end] != '.') end--;
  28. /* If we don't have a dot in the file name, append the extension */
  29. if(end < 1 || name[end] != '.')
  30. {
  31. strcpy(output, name);
  32. strcat(output, suffix);
  33. }
  34. /* If we found a dot before the last slash, replace the extension */
  35. else
  36. {
  37. memcpy(output, name, end);
  38. strcpy(output + end, suffix);
  39. }
  40. }