<commit_before>#include <stdio.h>
#include <stdlib.h>

#include "M8_interpreter.h"

#define M8_SIZE 8 /* Base size */
#define M8_MEMORY_SIZE (256)
#define M8_STACK_START 10

#define M8_REG_PC (regs.PC)
#define M8_REG_SP (regs.SP)
#define M8_REG_A (regs.A)
#define M8_REG_B (regs.B)
#define M8_REG_X (regs.X)
#define M8_REG_Y (regs.Y)
#define M8_REG_CC (regs.CC)


#define M8_MASK_C 1
#define M8_MASK_V 2
#define M8_MASK_Z 4
#define M8_MASK_N 8

#define M8_BIT_C (M8_REG_CC & M8_MASK_C)
#define M8_BIT_V ((M8_REG_CC & M8_MASK_V) >> 1)
#define M8_BIT_Z ((M8_REG_CC & M8_MASK_Z) >> 2)
#define M8_BIT_N ((M8_REG_CC & M8_MASK_N) >> 3)

#define M8_ENABLE(n)  (M8_REG_CC |= 1 << n)
#define M8_DISABLE(n) (M8_REG_CC &= ~(1 << n))

#define M8_ENABLE_C  M8_ENABLE(0)
#define M8_DISABLE_C M8_DISABLE(0)

#define M8_ENABLE_V  M8_ENABLE(1)
#define M8_DISABLE_V M8_DISABLE(1)

#define M8_ENABLE_Z  M8_ENABLE(2)
#define M8_DISABLE_Z M8_DISABLE(2)

#define M8_ENABLE_N  M8_ENABLE(3)
#define M8_DISABLE_N M8_DISABLE(3)

#define M8_BIT_SEVEN(x) (x>>7 & 1)

#define M8_MIN 0
#define M8_MAX 255

M8_Registers regs;
uint8_t memory[M8_MEMORY_SIZE] = {LDA, 6, LDB, 4, NOP, TRF, A, B, CLRA, CMPB, 7, STOP};
int running = 1;

uint8_t* M8_getregister(M8_enum_Registers r_enum){
  uint8_t* r = NULL;
  switch (r_enum) {
    case A:  r=&M8_REG_A;  break;
    case B:  r=&M8_REG_B;  break;
    case X:  r=&M8_REG_X;  break;
    case Y:  r=&M8_REG_Y;  break;
    case SP: r=&M8_REG_SP; break;
    case PC: r=&M8_REG_PC; break;
    case CC: r=&M8_REG_CC; break;
  }
  return r;
}


void M8_printregisters(void) {
    printf("Registers:\n");
    printf("A: %d\n", M8_REG_A);
    printf("B: %d\n", M8_REG_B);
    printf("X: %d\n", M8_REG_X);
    printf("Y: %d\n", M8_REG_Y);
    printf("SP: %d\n", M8_REG_SP);
    printf("PC: %d\n", M8_REG_PC);
    printf("CC: %d\n", M8_REG_CC);
}

void M8_printflags(void) {
    printf("Flags:\n");
    printf("C: %d\n", M8_BIT_C);
    printf("V: %d\n", M8_BIT_V);
    printf("Z: %d\n", M8_BIT_Z);
    printf("N: %d\n", M8_BIT_N);
}

void M8_printstate(void) {
  printf("------------------");
  M8_printregisters();
  M8_printflags();
}

void M8_setflags(int16_t result, uint8_t op1, uint8_t op2) {
  if (result < M8_MIN || result > M8_MAX) {
    M8_ENABLE_C;
  } else {
    M8_DISABLE_C;
  }

  if (!M8_BIT_SEVEN(result) && M8_BIT_SEVEN(op1) && M8_BIT_SEVEN(op2) ||
      M8_BIT_SEVEN(result) && !M8_BIT_SEVEN(op1) && !M8_BIT_SEVEN(op2) ||
      result < M8_MIN || result > M8_MAX){
    M8_ENABLE_V;
  } else {
    M8_DISABLE_V;
  }

  if (result < M8_MIN) {
    M8_ENABLE_N;
  } else {
    M8_DISABLE_N;
  }

  if (!result) { /* == 0 */
    M8_ENABLE_Z;
  } else {
    M8_DISABLE_Z;
  }
}

void M8_cmp(uint8_t r) {
  uint8_t op = memory[M8_REG_PC+1];
  int16_t temp = r - op;
  M8_setflags(temp, r, op);
  M8_REG_PC++;
}

void M8_clr(uint8_t *r) {
  *r = 0;
  M8_ENABLE_Z;
}

void M8_inc(uint8_t *r) {
  *r++;
  int16_t temp = *r;
  M8_setflags(temp, *r, 1);
}

void M8_load(uint8_t *r) {<commit_msg>Added parenthesis around an expression due to operator precedence<commit_after>#include <stdio.h>
#include <stdlib.h>

#include "M8_interpreter.h"

#define M8_SIZE 8 /* Base size */
#define M8_MEMORY_SIZE (256)
#define M8_STACK_START 10

#define M8_REG_PC (regs.PC)
#define M8_REG_SP (regs.SP)
#define M8_REG_A (regs.A)
#define M8_REG_B (regs.B)
#define M8_REG_X (regs.X)
#define M8_REG_Y (regs.Y)
#define M8_REG_CC (regs.CC)


#define M8_MASK_C 1
#define M8_MASK_V 2
#define M8_MASK_Z 4
#define M8_MASK_N 8

#define M8_BIT_C (M8_REG_CC & M8_MASK_C)
#define M8_BIT_V ((M8_REG_CC & M8_MASK_V) >> 1)
#define M8_BIT_Z ((M8_REG_CC & M8_MASK_Z) >> 2)
#define M8_BIT_N ((M8_REG_CC & M8_MASK_N) >> 3)

#define M8_ENABLE(n)  (M8_REG_CC |= 1 << n)
#define M8_DISABLE(n) (M8_REG_CC &= ~(1 << n))

#define M8_ENABLE_C  M8_ENABLE(0)
#define M8_DISABLE_C M8_DISABLE(0)

#define M8_ENABLE_V  M8_ENABLE(1)
#define M8_DISABLE_V M8_DISABLE(1)

#define M8_ENABLE_Z  M8_ENABLE(2)
#define M8_DISABLE_Z M8_DISABLE(2)

#define M8_ENABLE_N  M8_ENABLE(3)
#define M8_DISABLE_N M8_DISABLE(3)

#define M8_BIT_SEVEN(x) (x>>7 & 1)

#define M8_MIN 0
#define M8_MAX 255

M8_Registers regs;
uint8_t memory[M8_MEMORY_SIZE] = {LDA, 6, LDB, 4, NOP, TRF, A, B, CLRA, CMPB, 7, STOP};
int running = 1;

uint8_t* M8_getregister(M8_enum_Registers r_enum){
  uint8_t* r = NULL;
  switch (r_enum) {
    case A:  r=&M8_REG_A;  break;
    case B:  r=&M8_REG_B;  break;
    case X:  r=&M8_REG_X;  break;
    case Y:  r=&M8_REG_Y;  break;
    case SP: r=&M8_REG_SP; break;
    case PC: r=&M8_REG_PC; break;
    case CC: r=&M8_REG_CC; break;
  }
  return r;
}


void M8_printregisters(void) {
    printf("Registers:\n");
    printf("A: %d\n", M8_REG_A);
    printf("B: %d\n", M8_REG_B);
    printf("X: %d\n", M8_REG_X);
    printf("Y: %d\n", M8_REG_Y);
    printf("SP: %d\n", M8_REG_SP);
    printf("PC: %d\n", M8_REG_PC);
    printf("CC: %d\n", M8_REG_CC);
}

void M8_printflags(void) {
    printf("Flags:\n");
    printf("C: %d\n", M8_BIT_C);
    printf("V: %d\n", M8_BIT_V);
    printf("Z: %d\n", M8_BIT_Z);
    printf("N: %d\n", M8_BIT_N);
}

void M8_printstate(void) {
  printf("------------------");
  M8_printregisters();
  M8_printflags();
}

void M8_setflags(int16_t result, uint8_t op1, uint8_t op2) {
  if (result < M8_MIN || result > M8_MAX) {
    M8_ENABLE_C;
  } else {

    M8_DISABLE_C;
  }

  if (!M8_BIT_SEVEN(result) && M8_BIT_SEVEN(op1) && M8_BIT_SEVEN(op2) ||
      M8_BIT_SEVEN(result) && !M8_BIT_SEVEN(op1) && !M8_BIT_SEVEN(op2) ||
      result < M8_MIN || result > M8_MAX){
    M8_ENABLE_V;
  } else {
    M8_DISABLE_V;
  }

  if (result < M8_MIN) {
    M8_ENABLE_N;
  } else {
    M8_DISABLE_N;
  }

  if (!result) { /* == 0 */
    M8_ENABLE_Z;
  } else {
    M8_DISABLE_Z;
  }
}

void M8_cmp(uint8_t r) {
  uint8_t op = memory[M8_REG_PC+1];
  int16_t temp = r - op;
  M8_setflags(temp, r, op);
  M8_REG_PC++;
}

void M8_clr(uint8_t *r) {
  *r = 0;
  M8_ENABLE_Z;
}

void M8_inc(uint8_t *r) {
  (*r)++;
  int16_t temp = *r;
  M8_setflags(temp, *r, 1);
}

void M8_load(uint8_t *r) {
{'fraction_non_alphanumeric': 0.16295563252515258, 'fraction_numerical': 0.05442850074220683, 'mean_word_length': 3.820349761526232, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '723652', 'n_tokens_mistral': 3117, 'n_tokens_neox': 3046, 'n_words': 584}