Jump to content
  • Advertisement
Sign in to follow this  

Getting overflow in vb.net

This topic is 3790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a chip 8 emulator and i'm trying to read in a rom into memory but I keep getting an overflow. I'm reading one character at a time into an array of bytes.
Public Class Form1
    Dim ram(&HFFF) As Byte
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Sub ReadFile()
        Dim ofile As IO.File
        Dim oRead As IO.StreamReader
        Dim i As Integer
        i = 0

        oRead = ofile.OpenText("c:\roms/c8games/UFO")
        While Not oRead.EndOfStream

            ram(i) = oRead.Read()

            i += 1
        End While
     End Sub
End Class
[Edited by - ed209 on March 3, 2008 9:31:42 AM]

Share this post

Link to post
Share on other sites
A number of problems here; you open the stream as text (the files you are reading are binary); you do not dispose the opened stream; no bounds checking on i.

Rather than correct those, there are methods in the class library that make your life easier, and so here's a simpler version! [smile]

Sub ReadFile(ByVal filename As String)

' Clear the contents of Ram.
Array.Clear(Ram, 0, Ram.Length)

' Now read the entire source file into an array of bytes.
Dim ProgramFile As Byte() = IO.File.ReadAllBytes(filename)

' The largest program file we can fit in memory is between address &H200 and &HFFF.
' This is a maximum of &HE00 bytes, which is 3584.
' In case the specified file is too large, resize it:
If ProgramFile.Length > 3584 Then
Array.Resize(ProgramFile, 3584)
End If

' Finally, we need to copy our program to &H200 in memory.
Array.Copy(ProgramFile, 0, Ram, &H200, ProgramFile.Length)

End Sub

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!