Remix.run Logo
unleaded 11 hours ago

I had a strange AI experience the other day. I asked Kimi K2 to write a 6809 indexed operand decoder in C, and this is what it gave:

  uint16_t ea_indexed(void)
  {
      uint8_t post = *PC++;
      uint16_t base, off = 0;

      /* 1. pick base register */
      static const uint16_t *const base_tbl[4] = { &X, &Y, &U, &S };
      base = *base_tbl[(post >> 5) & 3];

      /* 2. work out the effective address */
      if ((post & 0x80) == 0) {                /* 5-bit signed offset */
          off = (int8_t)(post << 3) >> 3;
      } else if ((post & 0x60) == 0x20) {      /* 8- or 16-bit offset */
          if (post & 0x10) {                   /* 16-bit */
              off = (int16_t)fetch_be16(PC);
              PC += 2;
          } else {                             /* 8-bit */
              off = (int8_t)*PC++;
          }
      } else if ((post & 0x60) == 0x40) {      /* auto inc/dec */
          int8_t step = ((post & 0x0F) == 0x0) ? 1 :
                        ((post & 0x0F) == 0x1) ? 2 :
                        ((post & 0x0F) == 0x2) ? -1 :
                        ((post & 0x0F) == 0x3) ? -2 : 0;
          if (step > 0) base += step;          /* post-increment */
          off = step < 0 ? step : 0;           /* pre-decrement already applied */
          if (step < 0) base += step;
      } else if ((post & 0x60) == 0x60) {      /* accumulator offset */
          static const uint8_t scale[4] = {1,1,2,1};   /* A,B,D,illegal */
          uint8_t acc = (post >> 3) & 3;
          if (acc == 0) off = A;
          else if (acc == 1) off = B;
          else if (acc == 2) off = (A<<8)|B;   /* D */
          off *= scale[acc];
      } else {                                   /* 11x111xx is illegal */
          illegal();
      }

      uint16_t ea = base + off;

      /* 3. optional indirect */
      if (post & 0x10) ea = read16(ea);

      return ea;
  }
( full convo: https://text.is/4ZW2J )

From looking at Page 150 of https://colorcomputerarchive.com/repo/Documents/Books/Motoro... it looked pretty much perfect except for the accumulator addressing. That's impressive...

Then in another chat I asked it "give a technical description of how the 6809 indexed operands are decoded" and it just can't do it. It always gets the fundamentals wrong and makes pretty much everything up. Try it yourself, doesn't have to be Kimi most other AIs get it wrong too.

My assumption is that it's learned to how to represent it in code from reading emulator sources, but hasn't quite mapped it well enough to be able to explain it in English.. or something like that.*