Is the following ChatGPT-generated SNES code legit?

Discussion of hardware and software development for Super NES and Super Famicom. See the SNESdev wiki for more information.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: Is the following ChatGPT-generated SNES code legit?

Post by Oziphantom »

for in-depth from the basics tutorials see viewtopic.php?t=24387
nitro2k01
Posts: 252
Joined: Sat Aug 28, 2010 9:01 am

Re: Is the following ChatGPT-generated SNES code legit?

Post by nitro2k01 »

Revenant wrote: Sun Apr 09, 2023 12:26 pm Even if it did use WAI instead of WDM there, it'd still be wrong because DMA halts the CPU until it's finished. The WAI instruction would just stall (possibly forever) until a completely unrelated interrupt happened instead.
It's wrong, but it's a sensible idea if you tried to dream up a fanfic solution from partial knowledge. Hardware that fires an IRQ when DMA is finished is not unheard of, broadly speaking. Using a reserved opcode to wait for DMA to finish is just weird, though. Although another possibility is that it thought if WAI=wait for interrupts, WDM is wait for DMA.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Is the following ChatGPT-generated SNES code legit?

Post by Pokun »

SNES AYE wrote: Sun Apr 09, 2023 5:11 pm Now I'm totally wondering if there's a load of people working NES stuff right now who ultimately plan to move onto the SNES eventually at some point, but they just needed to start on something a bit simpler first. I actually hope that is the case to be honest, because I really want to see more brand new game for the SNES.

I guess maybe we'll all find out in about a year or two. ;-)
I think many people do that, I started with NES myself partly for that reason. I really wanted to program the SNES at some point because it's one of my all-time favorite consoles and can do more colorful games than NES, but I choose to start with NES because I thought that learning an assembly language would be very hard (I only knew some high level languages like C++) and I thought that starting with NES would make the most sense. It was not JUST a stepping stone however, I grew up with the NES and it was my first video game system (except perhaps Game & Watch), and I thought it would be easier to start with a system I had a passion for than going for Commodore 64 or something which I never owned (if you did, the C64 is probably a good way to learn 6502 though). So I ultimately picked the NES because it was a system I wanted to learn.

It was pretty hard in the beginning so it turned out to be a very good decision. After I knew the NES well enough I started with the Game Boy then MSX (after learning Z80), SNES, PC-Engine, Sega Master System and I dabbled in many other consoles as well including some MIPS for the Nintendo 64. After you learn one system it gets much easier to learn another, and since 65816 is backwards-compatible with 6502 you don't really have to learn a new assembly language, basically just a number of new (optional) instructions that can help speed things up.
All the systems listed above uses some variant of MOS 6502 or Zilog Z80 (except Nintendo 64 which uses MIPS) and those are the two most popular assembly languages from the time period, many famous programmers grew up learning either of these two or both.
User avatar
toggle switch
Posts: 139
Joined: Fri Sep 30, 2016 8:57 pm

Re: Is the following ChatGPT-generated SNES code legit?

Post by toggle switch »

i have asked chatGPT to code a few tasks in 6502 as a test. it gave mostly correct, but badly programmed (inefficient) answers. even when i specifically pointed to instructions that did nothing but waste CPU time it was unable to remove them in a way that makes sense.

and that was for simple tasks, like "SET register A to 64 if bit 1 of a memory location is on, and to zero if not". i imagine for anything worth doing it would fail, and fail badly. it will (in my experience) never find solutions that require clever manipulation of bit data using rotate or shift functions. in general my own code (which is not great) is easily much better than that generated by chatGPT

GPT4 seems like it will be better in that regard

here's some code based on the task above, generated by chatGPT

Code: Select all

; Define the macro
    .macro SET_A_IF_ODD address

        LDA \address     ; Load the value from the specified memory address into the A register
        AND #$01         ; Mask the low bit to determine if the value is odd
        BNE set_a        ; Branch to set_a if the value is odd
        LDA #0           ; Otherwise, set A to 0
        RTS              ; Return from the macro

    set_a:
        LDA #64          ; Set A to 64
        RTS              ; Return from the macro

    .endmacro
here for example, that LDA #0 is a completely pointless instruction, because A will already equal 0 if the code reaches that point. i asked it to optimize and it said it could not without more help. so i specifically mentioned that LDA #0 wasn't doing anything, and it gave me this reply:

Code: Select all

; Define the optimized macro
    .macro SET_A_IF_ODD address
        LDA \address     ; Load the value from the specified memory address into the A register
        AND #$01         ; Mask the low bit to determine if the value is odd
        BEQ clear_a      ; Branch to clear_a if the value is even
        LDA #64          ; Otherwise, set A to 64
        RTS              ; Return from the macro
    clear_a:
        CLC              ; Clear the carry flag (which also clears the accumulator)
        RTS              ; Return from the macro
    .endmacro
so it simply replaced one pointless instruction with another (in this case, CLC). the comment on that line is adorable! "clear the carry flag (which also clears the accumulator)" :roll: does it, though??
User avatar
Individualised
Posts: 310
Joined: Mon Sep 05, 2022 6:46 am

Re: Is the following ChatGPT-generated SNES code legit?

Post by Individualised »

Something I like to do is ask ChatGPT to translate code from one language to another, even from languages that are totally different from each other. I gave it a standard nested FOR loop written in QBasic that shifts down a 2D array of size 11x6 and asked it to convert it to 6502 and it gave me this:
ChatGPT wrote: Here is one possible translation of the code into 6502 assembly language. This assumes that the Board array is stored in memory starting at address $0200 and that the X and Y registers are free to use:

Code: Select all

; First loop: shift columns to the right
    LDA #$01    ; initialize K to 1
.loop1:
    CMP #$07    ; check if K > 6
    BEQ done    ; if so, exit loop
    LDX #$0B    ; initialize L to 11
.loop2:
    CMP #$FF    ; check if L < 0
    BEQ .nextk  ; if so, move on to next column
    LDA Board,X ; load value from column 6-K, row L
    STA Board+1,X ; store value in column 7-K, row L
    DEX         ; decrement L
    JMP .loop2  ; repeat for all rows
.nextk:
    INX         ; increment K
    JMP .loop1  ; repeat for all columns

; Second loop: clear leftmost column
    LDY #$00    ; initialize K to 0
.loop3:
    CMP #$0C    ; check if K >= 12
    BEQ done    ; if so, exit loop
    LDA #$00    ; set cell value to 0
    STA Board,Y ; store value in leftmost column, row K
    INY         ; increment K
    JMP .loop3  ; repeat for all rows
.done:
    ; code to continue after the loops finish
Note that this code assumes that the Board array is stored in row-major order, meaning that the values for each row are stored consecutively in memory. If the array is stored in column-major order instead, the indexing and looping logic would need to be adjusted accordingly. Additionally, the specific memory addresses used in this code may need to be adjusted depending on the size and location of the Board array in memory.
I have no idea if this is efficient or if it would even work. But I can already spot nonsense so I don't think it does.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Is the following ChatGPT-generated SNES code legit?

Post by Pokun »

Yeah it won't work, without reading it all I can tell it's mixing up K and L early on. It uses CMP when it means CPX and INX when it meant to increment K (which is not in X).

And in Toggle Switch's post:

Code: Select all

"RTS              ; Return from the macro"
it seems to mix up macro and subroutines. I mean that could work if you expand the macro in the place of a subroutine's location, but that's probably not normally how you would do it, and it sounds more like it thought a macro is a subroutine. It's impressive reasoning for a machine though, and for a subject that doesn't need to be as precise as computer programming it might have better results.
creaothceann
Posts: 611
Joined: Mon Jan 23, 2006 7:47 am
Location: Germany
Contact:

Re: Is the following ChatGPT-generated SNES code legit?

Post by creaothceann »

Pokun wrote: Wed Apr 12, 2023 3:27 pm It's impressive reasoning for a machine though
Is it really reasoning? I think the current approach is not 'understanding' but still 'educated guessing'.
My current setup:
Super Famicom ("2/1/3" SNS-CPU-GPM-02) → SCART → OSSC → StarTech USB3HDCAP → AmaRecTV 3.10
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Is the following ChatGPT-generated SNES code legit?

Post by Pokun »

But isn't that what reasoning is? Consciously processing the knowledge you possess in order to produce a logical solution to a problem. Although I guess the word "guess" might imply that you are gambling a bit with too little data and/or brain power to come to a conclusion you are really confident in, so yeah I guess that's still not quite "understanding".
93143
Posts: 1717
Joined: Fri Jul 04, 2014 9:31 pm

Re: Is the following ChatGPT-generated SNES code legit?

Post by 93143 »

Pokun wrote: Wed Apr 12, 2023 5:12 pmBut isn't that what reasoning is? Consciously processing the knowledge you possess in order to produce a logical solution to a problem.
It's not doing that. It's just a Chinese room.
User avatar
toggle switch
Posts: 139
Joined: Fri Sep 30, 2016 8:57 pm

Re: Is the following ChatGPT-generated SNES code legit?

Post by toggle switch »

right, the RTS instructions are gibberish as well. when you consider that the entire macros is ~15 cycles to execute, and 2 of them are wasted, and 6 of them are not only wasted, but also used to create a bug.... that's not great. hopefully things improve on that front, as i'd much rather spend time designing levels than twiddling bits.
Drag
Posts: 1615
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: Is the following ChatGPT-generated SNES code legit?

Post by Drag »

Try asking GPT for some pseudocode, or for the general steps for how a certain feature needs to function in order for it to work. It's possible that it can produce a reasonable overview or outline for something, but yeah, I wouldn't trust it to write a working implementation. :P

The reason is, there's a billion and one different ways you can write code to, for example, copy bytes into VRAM, but for the most part, the process will usually be "figure out which address in VRAM corresponds to the X/Y coordinates on the screen you want your tiles to appear, then write that address to register FOO, then write your string of bytes to register BAR."

Therefore, the more general you get, the higher the chance that GPT will give you something you can work with.
AAoK
Posts: 1
Joined: Sun Jun 04, 2023 3:18 am

Re: Is the following ChatGPT-generated SNES code legit?

Post by AAoK »

Made a profile so I can chime into this conversation.

I too tried to use the ChatGPT for getting started with the SNES code, and indeed haven't been successful. A little bit of background: I also have a special interest in 16-bit Nintendo, which comes and goes. I have tried a couple of times to get started, and I have a basic understanding of assembly language and the particularities of the console. I have a professional background in development with modern languages (JavaScript), but I find the learning curve extremely steep to get properly started with 16-bit.

As my last learning attempt was a couple of years ago, I wanted to try if ChatGPT would speed this process - and I am very fascinated by the idea of new technology creating "old" technology. This change has happened during my lifetime. So, during the past couple of days, I put a total of 7-8 hours into it.

My experience with the ChatGPT and SNES dev this far is that it helped tremendously to set up the development environment, and also very accurately explain, why something wasn't working. I had to refresh my memory on all of the steps, but the AI very quickly ran me through the process (around 45 minutes). However, when trying to actually program something, I hit a wall. For the record, I use WLA DX.

These are the observations I have made while using ChatGPT:
  • ChatGPT does not give functioning "hello world" code out of the box. It very much dismissed the headers and initialisation.
    It can explain the concepts quite well, but when programming, the structure is often coherent (this coming from a person who does not really know assembly, but has some kind of engineering background)
    It does a lot of guessing and runs in circles, which can make the process actually more frustrating than trying to figure out the issues yourself
    It speaks with a lot of confidence, even when it actually gives a wrong output, or even contradicts its own earlier proposals. This is understandable, as ChatGPT mimics written language, but this can be deceiving
    Related to the previous point, it can be very difficult to trust ChatGPT output, when you don't know what is nonsense and what is not. Thus, basic knowledge would help a lot
    As ChatGPT is a language model, it quite often makes assumptions of the compiler that are not accurate, eg. mixing syntax of different compilers
    For assembly code, thus far the best usage for me has indeed been fetching information quickly (eg. details about opcodes), reminding me about the concepts, and rubber ducking
This said, I have also done AI-assisted JavaScript development this week, and it is a brilliant helper for that. It quickly creates boilerplate code, and if you point out the mistakes for ChatGPT, it also corrects itself quite efficiently. There are other frustrating aspects (eg. modifying parts of code that you never asked it to modify), but I believe those can be resolved simply by learning how to use the tool.

The original problem, the steep learning curve of assembly and SNES, is a whole different topic, and although there has been a lot of good input in this thread, I would also like to bring one point to the table. I really miss a repository of simple samples, components, and demos with good explanations. Maybe I have failed to find one, but this library could consist of samples of how to loop (eg. color change on screen), controller input, how to move a sprite, simple animations, music module, etc. As little code as possible, and one simple functionality each.

I would be happy if someone can point me to such a resource, or confirm such a thing does not exist. In the latter case, I could even consider participating in a project and creating one (somehow, considering I don't really have the skills).

I have also been thinking that mentoring would be great. But I don't assume there are fluent SNES devs available a lot. But eg. a community online calls and hacking could help get started.

Sorry for the long post. In general, very happy to continue discussing the AI-assisted development.
SNES AYE
Posts: 201
Joined: Mon Nov 07, 2022 11:28 am

Re: Is the following ChatGPT-generated SNES code legit?

Post by SNES AYE »

AAoK wrote: Sun Jun 04, 2023 9:40 am The original problem, the steep learning curve of assembly and SNES, is a whole different topic, and although there has been a lot of good input in this thread, I would also like to bring one point to the table. I really miss a repository of simple samples, components, and demos with good explanations. Maybe I have failed to find one, but this library could consist of samples of how to loop (eg. color change on screen), controller input, how to move a sprite, simple animations, music module, etc. As little code as possible, and one simple functionality each.

I would be happy if someone can point me to such a resource, or confirm such a thing does not exist. In the latter case, I could even consider participating in a project and creating one (somehow, considering I don't really have the skills).

I have also been thinking that mentoring would be great. But I don't assume there are fluent SNES devs available a lot. But eg. a community online calls and hacking could help get started.

Sorry for the long post. In general, very happy to continue discussing the AI-assisted development.
I don't know if any of those exist, but they all sounds like great ideas to me, and I would also totally appreciate and welcome them. :)

To be really open and honest, I think a proper SNES programming series at the level of quality and ease of access to newcomers as the Retro Game Mechanics Explained for SNES video series on YouTube (the best case) or Pigsy's Retro Game Dev Tutorials for Genesis (my second choice) would be some kind of miracle ideal. And even a web page that's as well put together, concise, and visually-intuitively presented as the Raster Scroll online guide series of articles is for Genesis [but for SNES] would be great.

I've heard Retro Game Mechanics Explained is thinking about doing SNES coding tutorials at some point in the future, so fingers crossed on that one. . . .
Post Reply