Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:35:37+00:00 2026-05-28T23:35:37+00:00

I thougth I knew Python enough to program a basic chip8 emulator, but it

  • 0

I thougth I knew Python enough to program a basic chip8 emulator, but it seems I was wrong.

I’m basically having problems to read/write bytes to and from memory.
At a certain point in the program, I need to read to a variable a single byte I’ve already read from a file and stored in a list.
This is how I read from the file:

byte = rom.read(1)
memory.append(byte)
while byte:
    byte = rom.read(1)
    memory.append(byte)
rom.close()

At a certain point in the program, I need to get a byte from memory, AND it with 0x80 shifted by certain numbers (to extract single bits), so I use:

for i in range(h):
    p = ord(memory[addressI + i])
    for j in range(8):
        if(p & (0x80 >>j)) != 0:
        ...

It works fine for a while but then I get this error out of nowhere:

 (...)p = ord(memory[addressI + i])
 TypeError: ord() expected string of length 1, but int found

If I simply don’t use the ord funcion I get this error, as expected:

TypeError: unsupported operand type(s) for &: 'str' and 'int'

I think I get this error because at some point in the program I store the BCD of a stored value in in the memory. I guess it replaces the byte type previously stored for an int one.

data = registers[sNum>>8]
memory[addressI] = data/100
memory[addressI + 1] = (data/10) % 10
memory[addressI + 2] = data % 10

Possible solutions are:
1 – properly store the int calculated as a single byte in the list that “emulate” the chip8 memory
2 – use a function or a miracle instead of ord() that gets whatever the data is and stores it as a single byte

Any help would be appreciated.
By the way, I’m using windows and tested this code in both Python 2.7.2 and 3.2.2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:35:37+00:00Added an answer on May 28, 2026 at 11:35 pm

    I think it makes the most sense to keep the type in your list consistent, which you can do fairly easily by passing the integers that your are calculating into the chr() function (which is the reverse of ord()):

    data = registers[sNum>>8]
    memory[addressI] = chr(data/100)
    memory[addressI + 1] = chr((data/10) % 10)
    memory[addressI + 2] = chr(data % 10)
    

    However here is an alternative, which is to create a function that returns the right thing whether the argument is an int or a byte.

    ord_ = lambda c: c if isinstance(c, int) else ord(c)
    

    and then change the line in your for loop to this:

    p = ord_(memory[addressI + i])
    

    So now if that item in the list is an integer, ord_(memory[addressI + i]) will return it unchanged, but if it is a byte then it will return the result of ord().

    edit: The empty string in your list may be there because of your read loop, try changing it to the following:

    byte = rom.read(1)
    while byte:
        memory.append(byte)
        byte = rom.read(1)
    

    This should prevent the situation where the last time through the loop rom.read(1) returns an empty string and then immediately appends it to memory before checking the while condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought I knew everything about encodings and Python, but today I came across
I thought I knew this, but today I'm being proven wrong - again. Running
I have a problem that I thought I knew how I can fix, but
I knew my code was dying somehow because of the other symptoms, but even
So.. I thought I knew mssql fairly well but this one query asked of
I thought I knew how to declare javascript arrays but in this script I
I thought I knew how to deal with memory leaks and arrays, but then
I thought that I knew how to use fast enumeration, but there is something
I've thought I knew how to write basic regex. On my x64 pc in
A large amount of what I thought I knew about REST is apparently wrong

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.