Jack Palevich 的个人资料GrammerJack日志 工具 帮助
2006/12/24

Linus Torvalds on CPUs

Linus Torvalds is famous as the "Martin Luther" of the Linux OS, but recently I've discovered than one of his other interests is CPU design. And he knows quite a bit about x86 CPU architecture, both from the OS writer's point of view, and as an x86 CPU implementor, courtesy of his time at Transmeta.
 
Linus posts frequently on the Real World Tech forums. I've learned a lot of interesting points about modern CPU design from his posts.
 
The easiest way to keep track of Linus's posts is to use the Search the Forums page and search for "Linus".
 
His general thesis is that the x86 (at least starting with the 386) is a fine architecture, and that many x86 implementations provide excellent performance by making every case fast, not just some cases like other CPUs.
 
2006/12/11

How to Write XNA Game Studio Express Games with F# - Part 3: Running on the Xbox 360

Now to run on the 360. First, you need to set up your Xbox 360 and PC to run regular C# XNA games. That's beyond the scope of this article, but you should be able to find directions on the XNA Game Studio Express web site. I'm going to assume you've already created and run the sample C# games on your Xbox 360, so that you have everything configured correctly.

Now, in order to run your F# game on the Xbox 360, you'll need to make these changes:

  • Your F# source file will need to reference the Xbox 360 versions of the XNA GSE DLLs.
  • You'll need to compile your F# game DLL as a standalone DLL so that it doesn't try to reference other F# support DLLs that haven't been copied to the 360
  • You'll need to create an "Xbox 360 Game" XNA GSE project rather than a "Windows Game" XNA GSE project.

Let's go over that process in detail:

Step 1: Modify the F# game.fs file you created in Part 2:

Change this line (that's near the beginning) from the Windows path:

#I @"C:\Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\References\Windows\x86"

To the following lines, which conditionally use the correct path, depending upon whether we're compiling for Windows or for Xbox 360:

#if XBOX360
#I @"C:\Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\References\Xbox360"
#else
#I @"C:\Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\References\Windows\x86"
#endif

 Step 2: Compile the F# DLL as a standalone DLL:

  • Add the --standalone option to compile the DLL as a single DLL that doesn't need any F# runtime DLLs.
  • Use --define to define the symbol XBOX360 so that the proper #I path is referenced.
  • Use -o to give the Xbox 360 game DLL a different name. This makes it easier to develop for both platforms at once.
"C:\Program Files\FSharp-1.1.13.8\bin\fsc.exe" -a -g --standalone --define XBOX360 -o game360.dll game.fs 

 Step 3: Create an Xbox 360 Game XNA GSE project:

  1. Choose the menu item Start:All Programs:Microsoft XNA Game Studio Express:XNA Game Studio Express
  2. Choose "File:New Project"
  3. Choose "Xbox 360 Game"
  4. Click "OK"

Step 4: Add your F# DLL to the Xbox 360 Game XNA GSE project

  1. Choose the menu item "Project:Add Reference..."
  2. Choose the "Browse" tab from the "Add Reference" dialog
  3. Use the file dialog to select the "game360.dll file that you created in step 2.
  4. Press the "OK" button to dismiss the "Add Reference" dialog.

Step 5: Write some C# code to call your F# game

  1. Remove the "Game1.cs" file that was automatically added to your F# project. Do this by
    1. Right-click on "Game1.cs" in the "Solution Explorer" window, and choose "Delete" from the context menu
    2. Press the "OK" button in the delete confirmation dialog.
  2. Replace all the text in the Program.cs file. Do this by
    1. Double-click on "Program.cs" in the Solution Explorer
    2. Choose menu item "Edit:Select All
    3. Choose menu item "Edit:Delete"
  3. Add the following text:
namespace MyGame
{
    static class Program
    {
        static void Main(string[] args)
        {
            Game.main(); // Call our F# "main" function.
        }
    }
}

Step 6: Compile and run your game on the Xbox 360

  1. Choose the menu item "Build:Build Solution"
  2. Choose "Debug:Start Debugging"

That's all there is to it!

I hope you've enjoyed these posts. If you're interested in the F# language, a good resource is hubFS: THE place for F#

FAQ

Q: Can I use this call-a-DLL approach to write Xbox 360 XNA Game Studio Express games for other non-C# CLR languages like Iron Python or Visual Basic?

A: Unfortunately no, not for those two specific languages. (Sorry!)  The reason is that the Xbox 360 version of the XNA GSE CLR is based on the .NET Compact Framework 2.0. This is a problem for Iron Python and Visual Basic because those particular languages rely on CLR features that are not present in the .NET Compact Framework 2.0. Therefore you will get a runtime error when you try and run your Iron Python or Visual Basic code on the 360. It's possible that future versions of either the languages or the Xbox 360 GSE runtime will fix this problem. (But don't hold your breath -- nobody has promised anything in these areas. Only C# is officially supported for XNA GSE games.)

Note that this limitation to run on top of the .NET Compact Framework only applies to the Xbox 360 version of your game. Although I have not tried it, I believe that it should be possible to use a variation of the techniques in Part 2 to run Iron Python and/or Visual Basic XNA GSE games on Windows.

Alan Phipps has written set of tutorials on Using Visual Basic with XNA GSE on Windows

Q: Have you written an Xbox 360 game in F#?

A: Yes, a small game "Dandy Dungeon". I haven't had time to clean up the sources for publication yet. You can find the sources to an early non-XNA version of this game on hubFS.

Q: Why are you messing around with F# anyway? Isn't C# cool enough for you?

A: I love C# (and I do most of my day-job programming in either C# or C++), but I'm attracted to F# for it's brevity and its neat-o functional programming language features. It's also a lot of fun to watch the language evolve and improve.

How to Write XNA Game Studio Express Games with F# - Part 2 Using F# with Visual C# Express

In Part 1 we saw how to create a simple stand-alone F# game based on XNA GSE. But the stand-along game had two limitations:

  • It couldn't use the XNA GSE Content Pipeline to build art assets.
  • It couldn't be deployed to the Xbox 360.

The reason for both of these limitations is that the approach we took avoided using Visual C# Express. In order to get around the limitations, we have to bring Visual C# Express into the picture.

Unfortunately, we can't just create an F# project in Visual C# Express. For one reason or another, Visual C# Express only supports compiling C# source files. However, there is an escape hatch: Visual C# Express allows you to reference any number of DLLs, and those DLLs can be written in any language. Even better, the Visual C# debugger works fine with DLLs written in any .NET language. So our strategy is:

  • Compile our F# code as a DLL.
  • Create a Visual C# Express XNA GSE project
  • Add a Reference to our F# game DLL
  • Call the F# game code from the main C# file.

That's all there is to it! Let's go through the process step by step in more detail:

Step 1: Configure Visual C# Express to expose advanced features:

  1. Choose the menu item Start:All Programs:Microsoft XNA Game Studio Express:XNA Game Studio Express
  2. Choose menu item "Tools:Options"
  3. Choose "Projects & Solutions:General"
  4. Check the "Show advanced build configurations" checkbox
  5. Press the "OK" button
  6. Choose the menu item "File:Exit"

Step 2: Write our F# code. Use the text editor of your choice to write the following F# code.

NOTE: This version of game.fs is slightly different than the game.fs file in the previous post. The difference is that we do not call the main() function at the end of the file. (Instead we will call it from the C# code in a C# project that we create in a later step.)

// An XNA game in F#. Part 2: As a DLL

#light

#I @"C:\Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\References\Windows\x86"

#r "Microsoft.Xna.Framework.dll"
#r "Microsoft.Xna.Framework.Game.dll"

// Not all of these opens are required for this sample, but you will need them to write a full game

open Collections
open Compatibility 
open Idioms
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Audio
open Microsoft.Xna.Framework.Content
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input
open Microsoft.Xna.Framework.Storage
open System
open System.IO

type MyGame = class
    inherit Game as base
    
    val mutable graphics : GraphicsDeviceManager
    val mutable content : ContentManager
    
    new() as this =
        {
            graphics = null
            content = null
        }
        then
            this.graphics <- new GraphicsDeviceManager(this)
            this.content <- new ContentManager(this.Services)
    
    override this.Draw(gameTime) =
        let gd = this.graphics.GraphicsDevice
        gd.Clear(Color.Green)

    end
    
let main() =
    let game = new MyGame()
    game.Run()

Step 3: Save the F# file as "game.fs", and then open a cmd.exe shell window and compile the game as a DLL using the F# compiler:

"C:\Program Files\FSharp-1.1.13.8\bin\fsc.exe" -a -g game.fs

The -a flag means "create an assembly", which is the CLR name for a DLL. The -g flag means "add debugging information", which lets you step through your F# code in the debugger.

Step 4: Create a Visual C# XNA Game Studio Express project

  1. Choose the menu item Start:All Programs:Microsoft XNA Game Studio Express:XNA Game Studio Express
  2. Choose "File:New Project"
  3. Choose "Windows Game"
  4. Click "OK"

Step 5: Add your F# DLL to the XNA GSE project

  1. Choose the menu item "Project:Add Reference..."
  2. Choose the "Browse" tab from the "Add Reference" dialog
  3. Use the file dialog to select the "game.dll file that you created in step 2.
  4. Press the "OK" button to dismiss the "Add Reference" dialog.

Step 6: Write some C# code to call your F# game

  1. Remove the "Game1.cs" file that was automatically added to your F# project. Do this by
    1. Right-click on "Game1.cs" in the "Solution Explorer" window, and choose "Delete" from the context menu
    2. Press the "OK" button in the delete confirmation dialog.
  2. Replace all the text in the Program.cs file. Do this by
    1. Double-click on "Program.cs" in the Solution Explorer
    2. Choose menu item "Edit:Select All
    3. Choose menu item "Edit:Delete"
  3. Add the following text:
namespace MyGame
{
    static class Program
    {
        static void Main(string[] args)
        {
            Game.main(); // Call our F# "main" function.
        }
    }
}

Step 7: Compile and run your game

  1. Choose the menu item "Build:Build Solution"
  2. Choose "Debug:Start Debugging"

That's all there is to it!

The benefits of this round-about approach are

  • You can use the Visual C# Express debugger to debug your F# code.
  • You can use the Content Pipeline in to build and load your art assets.
  • You can deploy to the Xbox 360.

Coming in part 3: Changes needed to support Xbox 360 development.

How to Write XNA Game Studio Express Games with F# - Part 1 Getting Started

This blog entry explains how to write XNA Game Studio Express games in the F# programming language. To begin, let me explain what "XNA GSE" and "F#" are:

  • XNA Game Studio Express is a product from Microsoft that enables you to write video games for the Windows and Xbox 360.
    • The tools are free.
    • Running XNA GSE games on Windows is free.
    • The only thing that's not free is that you need a "XNA Creators Club" subscription to run games on the 360.
  • F# (pronounced F-sharp) is a new programming language being developed by Microsoft Research
    • It's based on the ML language family, specificly the O'Caml dialect.
    • It runs on the .NET Common Language Runtime, the same as C# or Visual Basic.
    • It's briefer and more expressive than C#.
    • It's free, and it's regularly updated with powerful new features.

In this series of articles, I'm going to show how to:

  1. Part 1: Create a trivial F# XNA game that runs on Windows
  2. Part 2: Modify the game to work with the XNA Game StudioExpress IDE
  3. Part 3: Modify the game to on both Windows and Xbox 360

Let's get started with creating a trivial F# XNA game that runs on Windows. (And by trivial I mean really trivial: the game just draws a blank green screen.)

Step 1: Download and install Visual C# Express: Visual C# Express Home Page

Step 2: Download and install XNA Game Studio Express: XNA Game Studio Express Home Page

Step 3: Download and install F#: F# Home Page

Note: These articles are for F# version 1.1.13.8. Earlier version's won't work, later versions might work.

Step 4: Create an F# source file using your favorite text editor:

// An XNA game in F#

#light

#I @"C:\Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\References\Windows\x86"

#r "Microsoft.Xna.Framework.dll"
#r "Microsoft.Xna.Framework.Game.dll"

// Not all of these opens are required for this sample, but you will need them to write a full game

open Collections
open Compatibility 
open Idioms
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Audio
open Microsoft.Xna.Framework.Content
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input
open Microsoft.Xna.Framework.Storage
open System
open System.IO

type MyGame = class
    inherit Game as base
    
    val mutable graphics : GraphicsDeviceManager
    
    new() as this =
        {
            graphics = null
        }
        then
            this.graphics <- new GraphicsDeviceManager(this)
    
    override this.Draw(gameTime) =
        let gd = this.graphics.GraphicsDevice
        gd.Clear(Color.Green)

    end
    
let main() =
    let game = new MyGame()
    game.Run()

[<STAThread>]
do main()

 Step 5: Save the F# file as "game.fs", and then open a cmd.exe shell window and compile the game using the F# compiler:

"C:\Program Files\FSharp-1.1.13.8\bin\fsc.exe" game.fs

Step 6: Run the game by double-clicking on the "game.exe" or by typing "game.exe" at the command line.

That's all there is to it! However, is are some drawbacks to this approach. By bypassing Visual C# Express:

  • We lose the ability to use the Content Framework to build our game's art assets.
  • We lose the ability to run our game on the Xbox 360.

In the Part 2 of this article I will explain how to work around these problems.