Stephen Smith's Blog

Musings on Machine Learning…

Posts Tagged ‘Apple

Programming an Apple Watch

with 4 comments

Introduction

A cool thing about the Apple Watch is that it’s really a full ARM based computer running a Unix derived operating system that is fully programmable. Although most Apple Watch owners will never write programs for their Apple Watch, as they never write programs for their iPad or iPhone, it is entirely possible to do so using Apple’s Xcode development environment running on a newer Mac. In this article we’ll look a little at the powerful computer that is the Apple Watch and give an idea of how programs or Apps are developed.

The Platform

The Apple Watch contains a whole lot of processing power, combined with a ton of sensors and a nice retina display all packed into a very small package. The processor is a dual core 64-bit ARM CPU with 1Gig of RAM. In the Series 6 watch, these cores are low energy cores from the iPhone 11’s CPU. There is also 32Gig of storage for Apps and data. There is even a mini PowerVR GPU. The touch sensitive display is only 1.78”, but still has a resolution of 448 x 368 pixels. 1Gig may not sound like much RAM, but remember that all the Raspberry Pi’s up to the 3B, only had 1Gig of RAM and ran a full version of Linux quite nicely. For connectivity there is WiFi, Cell, Bluetooth and ultra wideband.

The sensors include: accelerometer, gyro, heart rate, barometer, always-on altimeter, compass, GPS, microphone, SpO2 and VO2max.

That’s quite a bit of computer packed into a small package only weighing 48grams.

Programming

The Apple Watch’s operating system is WatchOS which is based on iOS. Programming for WatchOS is pretty similar to programming for iOS and in fact you use the same tools. There is a WatchKit API for watch specific functions and you should keep in mind the Watchs UI limitations when creating Apps. For instance, even though you can do text entry on the watch, you have to draw each character or use the built in speech to text interface, i.e. there is no keyboard.

Typically you develop a WatchApp in parallel with an iPhone App, where the iPhone App provides configuration, setup and does much of the work allowing you to minimize the interface required on the watch. Xcode makes creating these dual Apps easy and in fact you can have separate heads for the Watch version, the Apple TV version, the iPhone version and the iPad version.

In my book “Programming with 64-Bit ARM Assembly Language”, I create a simple iOS App that has a text box, where you enter some text and then it calls an Assembly Language routine to convert the text to uppercase. Alex vonBelow took this example and added support for both the Apple Watch and AppleTV. The Github for this is available here and this program is in Chapter 10.

For most work, you debug by running the application in the iOS/WatchOS simulator. The nice thing about my new ARM based Mac is that the simulator is quite fast, since it doesn’t have to simulate running an ARM CPU on an Intel processor, instead everything is ARM and works quickly. Below is a screenshot of running this uppercase app for the Apple Watch.

The cool thing is that if you know how to write iOS Apps, then you already know how to write Apple Watch Apps (as well as AppleTV Apps). Besides writing code in Objective-C or Swift, you can even write code in 64-bit ARM Assembly Language. Xcode makes it easy to provide separate appropriate screens for each device.

There are tons of books on how to write iOS Apps and all that knowledge works across all the Apple mobile products. The key thing for the Watch is that the UI should be mostly informational and any UI should be limited to just a couple of buttons.

Programming with Objective-C or Swift using the iOS frameworks is fairly complex, it would be nice if there was something simpler like a version of Scratch for WatchOS or a command prompt App, like the one for the iPhone. But at least Xcode creates a reasonable skeleton working App when you create a new project.

Summary

The Apple Watch is quite a powerful little computer in its own right. You can program it from Xcode and use nearly all the tools you use for iOS development for the iPhone or iPad. It’s really amazing how much computing power, connectivity and sensors are stuffed into the small watch package.

Written by smist08

February 26, 2021 at 10:40 am

State of Software on Apple Silicon

leave a comment »

Introduction

Apple Silicon ARM based Macintoshes first shipped back on Nov. 20, 2020. We are now three months later, so I thought I’d review the state of software on these new Macs. Whenever a vendor changes the CPUs in their computers there is typically a lag between the hardware shipping and then software becoming available for the new platform. Apple took a number of extraordinary measures to try to eliminate this lag, to have a lot of software available when the first real hardware shipped. In this article we’ll look at what is available now, what is missing and how the journey seems to be going.

Rosetta and the Early Adopter Program

Knowing that software would be a problem when Apple switched from Intel to ARM for their Macintoshes, they worked hard to produce an excellent Intel emulation program called Rosetta. This allows most Intel based MacOS programs to run as is on the new hardware. I found this to work really well, with a couple of exceptions. One problem is battery life, when I first received my new MacBook Air, there wasn’t a native general media player yet, and running the Intel version of VLC would drain the battery in a couple of hours. Since then a native ARM version of VLC has been released and I’m able to play videos all day without recharging. Another problem is that Rosetta doesn’t emulate Intel AVX vector processing instructions and this prevents some machine learning libraries from running. However, I found most things did work and hence I could get everything I needed done, done.

The other thing Apple did was seed developers with an early prototype Mac Mini using an iPad Pro’s ARM CPU and a beta version of MacOS Big Sur. This allowed developers to get a jump on porting their applications to the new platform. The downside was that this hardware cost US $600 and you had  to sign a heavy nondisclosure along with agreeing to return this hardware when the real thing shipped. This limited the program to mostly larger companies who could easily afford this. At the end Apple provided a $500 Apple store credit if you returned the unit and if this was known at the beginning, more people might have participated.

Native Applications

There are now a large collection of native Apple Silicon applications available including Microsoft Office, Google Chrome, VLC Player and Zoom. Others such as Adobe Lightroom or Parallels virtualization software are available in beta. There is a list of native applications here.

Of course all Apple’s applications are available for the M1 chip including Xcode which gives a pretty robust starting point.

Porting applications from Intel to ARM appears to be fairly straightforward and expect any missing applications (as long as they are still actively developed) to show up soon.

MacOS is based on Unix as is Linux and Mac users are used to all the various Linux applications having MacOS versions as well. A great place to get these is from one of the MacOS package managers like Homebrew. Homebrew has ARM native versions of most open source programs, including GCC and lots of Python libraries.

Xcode installed the LLVM compiler collection and then with GCC you can download and build most open source projects if you really need to, but chances are the folk at Homebrew have beaten you to it.

Another source of native Apps are iOS iPad and iPhone apps, many of these will install on the new Macs and for a lot of things are the best way to go.

Virtualization

If you want to run Linux or Windows on your new Apple Silicon Mac, one way to go is via virtualization. Parallels has a beta version of their software for Apple Silicon and I gave it a try and got a virtualized copy Ubuntu Linux up and running without any problems. The target operating system has to be 64-bit ARM based, which means you can choose from quite a few flavours of Linux.

Although bootcamp isn’t supported on the new Macs, you can run virtualized ARM based Windows using Parallels, but this version of Windows is only available through the Windows Insider program.

Other Operating Systems

There is a large contingent of Linux developers working hard to get Linux running natively on this new Apple hardware. Correlium has Ubuntu Linux running natively, booting either from the SSD or from a USB drive. This version supports much of the new Apple hardware, except it doesn’t use the GPU at all. Asahi Linux is working hard on GPU support, but that is a big job.

Apple is making this job hard but not providing documentation on the low level hardware interfaces, but people are making good progress reverse engineering MacOS.

The good news is that it is possible to run other OS’s like Linux and over time we’ll hopefully see other operating systems in addition to Linux ported as well.

What’s Missing

The big thing missing is support for the GPU and TPU. All the libraries that utilize GPUs for acceleration, won’t use the Apple GPU. This is a huge advantage that nVidia has, that nearly every machine learning library supports acceleration on nVidia GPUs, then next, but still lagging is support for AMD GPUs. 

Apple wants everyone to use their Metal API to access the GPU, but the problem is getting people to do this. Apple has taken a branch of Google’s Tensorflow and is adding Metal support themselves, but they have a bit of a tough road ahead to get good support across the board. They could try to help organizations like OpenCL write their drivers, but I think this will take time.

Another thing is to sort out the installation of systems like Python where there are hundreds of add-on libraries. There are lots of conda virtual installs for the new M1s but most mix and match installs are still a pain, and nowhere near as painless as they are on Intel based MacOS or on Linux.

Summary

There has been huge progress in porting software to the new ARM based Macs. There is still work to do, but it appears the new Macs are selling well and there seem to be lots of developers acquiring these new Macs. Anyone serious about iOS development wants these as it is so much easier having your development system running the same processor as your target. The missing pieces are becoming more and more obscure and most of the holes can be filled with programs running under Rosetta. I use my new MacBook for pretty much all my work and find everything I need already there and working really well.

Written by smist08

February 19, 2021 at 11:13 am

Posted in Business

Tagged with , , , ,

Apple Watch Series 6 Review

with 3 comments

Introduction

I recently celebrated another birthday and the present from my wife and some of her family was a brand new Apple Watch Series 6. Basically an Apple Watch is a little tiny iPhone, specialized to be a watch, but like an iPhone it can run Apps and has a lot of computing power. In this article we’ll discuss some of the watch’s salient features and I’ll give my initial impressions.

Unboxing

Sadly Apple uses UPS for shipping, so the watch arrived a few days after my birthday, I already complained about UPS quite a bit here. Unboxing consisted of removing the Apple packaging from the UPS packaging, removing lots of plastic wrap and opening the cardboard boxes within. Inside was the watch, a strap and a charging cable. There were the usual legal bits of paper warning about not changing the battery yourself and beware the choking hazard. There was a piece of paper with a picture and one sentence in 27 languages on how to put the strap on, turn it on and find the user manual on the Apple book store. Installing the strap is easy and it’s designed to be easily interchangeable, which is a nice improvement over my previous Garmin GPS watch where changing the strap was a real fiddly struggle.

Following the instructions to turn the watch on didn’t work, so I attached the charging cable, plugged it in and saw there it was 0% charged. The charging cable has a magnet on one end to attach to the back of the watch and the other is a USB-A connector. There is no included USB-A to AC power plug. Fortunately I have lots of these. Also I’m writing this on a new MacBook Air which does not have any USB-A ports, only USB-C ports, so without a hub, can’t be used, which I found strange that it didn’t come with a cable that would work with other Apple products.

Setup

After charging for ten or fifteen minutes, the watch had enough power to work while on the charger. To set it up, you use an iPhone, I have no idea how to do this if you don’t have an iPhone. Basically, if your iPhone is near the watch it detects it and launches the watch app. Then the watch displays a swirling pattern that you point the iPhones camera at. Once you have it correct in the field of view, the phone is paired to the watch. Then you use the iPhone App to go through the various setup steps, which is mostly confirming the EULA and how much access you want the watch to have to your stuff. You can skip most steps and set them up at any time from the App. I chose most of the defaults with a few exceptions:

  • I chose to manually select which Apps to download, otherwise it will install every App on your phone which has an iWatch version.
  • I didn’t enable the feature that would call 911 if the watch detects me falling. I’m worried about false alarms, and so far when I’ve fallen, I’ve been able to get up again.
  • I didn’t enable credit card payment. The watch has NFC and you can wave it over supported credit card devices to pay, but so far I haven’t integrated credit card payments to either my phone or watch.

Once done you are good to go. The watch takes about an hour and a half to fully charge, so when that was done I could put it on and tell time.

Impressions

The screen is easy to read, it’s always on, but it brightens when you tilt it towards you. I like the ease with which it measures heart rate, I’ve used lots of sports watches with the separate chest strap and those are such a pain that I rarely use it. There are lots of watch faces to choose from and you can easily customize any of them, however figuring out the correct thing to get what you want can be tricky (like displaying your heart rate rather than the heart rate App symbol). Apple hires good graphic designers, so all the faces are appealing and it’s a matter of choice whether you like lots or info or something clean, whether you like analog watch hands or a digital display.

How to run Apps baffled me at first, but once you get the hang of it, it is easy. It helps if you remember the icons of the Apps you use.

The watch is intrusive in some surprising ways, for instance if you don’t get up and move around every hour, it will bug you to do so. Similarly, it can be a bit aggressive on trying to get you to exercise. I’m often surprised by a new beep and notification from the watch. For now I’m ok with it, but I suspect in a week or two this will get old, and I’ll have to figure out how to turn some of these off.

The reason my Garmin watches failed, was that over the years their batteries held less and less charge. My current Garmin started off being able to track a hike of about six hours, but each year it got less, next I couldn’t record a hike, but a two hour bike rides was still ok, and recently it’s a bit problematic even recording a 45 minute run, So far the Apple Watch seems to be able to go for a day and a half between charges. I’m happy with this and I’ll be even happier if this keeps up for a number of years.

There are a lot of upsells associated with the Apple Watch. The Apple fitness App gives you basic functionality on your phone, but wants you to pay $13 or so per month for fitness+, so you have to say no to this a few times. Similarly the Strava App gives you basic functionality, but wants $80 per year for advanced things, the main thing missing being heart  rate tracking (but the basic Apple App does do this). So far I’ve avoided any of these upsells, but I might try the three month free trial for fitness+ at some point.

It would be nice if there were iPad versions of the iPhone iWatch Apps, but either there aren’t or most functionality is missing. It would be nice to be able to use the larger screen, but oh well.

After the first night, when I picked up my watch it complained that there was a new version of WatchOS, but it didn’t install since the watch wasn’t on the charger. No big deal, the next night I put the watch on the charger and went to bed. In the morning it had another error that the install failed because my iPhone wasn’t close enough to the watch. Anyway the third time was a charm, not really sure why the phone needs to be near the watch, since the watch does WiFi.

Summary

The Apple iWatch is a fun toy, I like that my watch tells me how much time I have until sunset and what my heart rate is. I’m not sure if this is legal, but I can glance at text messages while driving on my watch. I like the strava App in that I can start/stop activities and see my progress without getting my phone out of its exercise case. I’ve only had it for a couple of days, but I’ll write an update once I’ve been using it for a few weeks.

Written by smist08

February 5, 2021 at 4:57 pm

Posted in Business

Tagged with , , ,

Porting Linux to Apple Silicon

with 2 comments

Introduction

When Apple announced they were switching from Intel to ARM CPUs, there was a worry that Apple would lock out installing non-Apple operating systems such as Linux. There is a new security processor that people worried would only allow MacOS to boot on these new chips. Fortunately, this proved to be false and the new ARM based Macintoshes fully support booting homebrew operating systems either from the SSD or from USB storage. However, the new Apple M1 chips present a number of problems that we’ll discuss in this article as well as why so many people are so interested in doing this.

Linus Torvalds, the father of Linux, recently said that he wished the new MacBooks ran Linux and that he would consider this the ultimate laptop and really want one. Linus said he saw porting Linux as possible, but personally he didn’t have the time to commit.

Last week’s article on an Assembly Language “Hello World” program hit number 1 on Hacker News and based on the comments, the interest was largely generated by the challenge of porting Linux to these new Apple systems. As we’ll see, doing this is going to require both reverse engineering and then writing ARM 64-bit Assembly Language code.

Asahi Linux

Last week we saw the announcement of the Asahi Linux project. Asahi means “rising sun” in Japanese and “asahi ringo” is Japanese for Macintosh Apple. The goal of this project is to develop a version of Linux that fully supports all the new hardware features of the Apple M1 chip including the GPU and USB-C ports. This won’t be easy because even though Apple doesn’t block you from doing this, they don’t help and they don’t provide any documentation on how the hardware works. People already have character based Linux booting and running on the Apple M1 Macs, and you can run the regular ARM version of Linux under virtualization on these new Macs, but the real goal is to understand the new hardware and have a version of Linux talking directly to the hardware that uses all the capabilities, like the GPU, to run as well as or better than MacOS.

GPUs and Linux

GPUs have always been a sore point with the Linux community. None of the GPU vendors properly document their hardware APIs to their products and believe the best way to support various operating systems is to provide precompiled binaries with no source code. Obviously this roils the open source community. GPUs are complicated and change a lot with each hardware generation. Newer Intel and AMD CPUs all have integrated graphics that have good open source drivers that at least will work, but at the disadvantage of not using all the fancy hardware you paid for in your expensive gaming PC. Even the Raspberry Pi versions of Linux use a binary Broadcom drive for the integrated GPU, rather than something open source.

Over the years, intrepid Linux developers have reverse engineered how all these GPUs work, so there are open source drivers for most nVidia and AMD GPUs. In fact, since neither nVidia or AMD support their hardware for all that long, if you have a more than 10 year old graphics card and run Linux, then you are pretty much forced to use the open source driver or switch to Intel integrated graphics (if available) or just stop upgrading the operating system and drivers.

The good news is that the open source community has a lot of experience figuring out how GPUs work, including those from nVidia, AMD, ARM and Broadcom. The bad news is that it takes time to first work out a disassembler of the GPU instructions to go from the binary form and work out what each bit means to produce a mnemonic Assembly Language source form. Then once this is known, write an Assembler for this and then use the tool to create the graphics driver. The Apple GPU isn’t entirely new, originally it was based on Imagination Technologies GPU design and then went through several iterations in iPads and iPhones before the current newest version ending up in the M1. Hopefully this history will be some help in developing the new Linux drivers.

Leveraging Existing Drivers

All the CPU vendors including ARM Holdings are motivated to contribute to the Linux kernel to ensure it runs well on their hardware. Linux is big enough that it greatly benefits vendors adoption to have a solid Linux offering. There is already really good ARM support in the Linux kernel and its tool chain such as GNU GCC. This is a solid first step in producing a working version of Linux for Apple Silicon.

Further, Apple doesn’t do everything themselves. There is hope that even if components are integrated into the M1 SoC that they still used standard designs. After all, Apple didn’t want to write all new drivers for MacOS. Hopefully a lot of the hardware drivers for the Intel Macs will just need to be recompiled for ARM and just work (or require very little work).

I haven’t mentioned the Apple integrated AI processor, but the hope here is that once the GPU is understood, that the AI processor is fairly similar, just missing the graphics specific parts and containing the same core vector processor.

There are quite a few other components in the SoC including sound processing and video decoding, hopefully these are known entities and not entirely new.

Why Do All This Work?

It’s hard enough writing device drivers when you have complete hardware documentation and can call a vendor’s support line. Having to reverse engineer how everything works first is a monumental task, so why are all these open source developers flocking to this task? Quite a few people like the challenge, if Apple provided lots of good documentation, then it would just be too easy. There is an attraction to having to connect hardware diagnostic equipment to your computer and iteratively write Assembly Language to figure out how to control things. None of this work is paid, besides the odd bit of gofundme money, these are mostly volunteers doing this in their spare time separate from their day jobs.

Humans are very curious creatures. Apple, by not providing any details, has piqued everyone’s curiosity. We don’t like being told no, you’re not allowed to know something. This just irritates us and perhaps we think there is something good being withheld from us.

There is also some fame to be had in hacker circles, as the people who solve the big problems are going to become legends in the Linux world.

Whatever the reason, we will all benefit from their hard work and determination. A well running Linux on Apple Silicon will be a great way to get full control of your hardware and escape App store restrictions and Apple’s policies on what you can and cannot do with your computer. It might even be a first step to producing Linux for iPhones and iPads which would be cool.

Summary

Apple has set a mythic challenge to hackers everywhere. By not providing any hardware documentation, Apple has created an epic contest for hackers to crack this nut and figure out how all the nitty gritty details of Apple Silicon work. This is a fun and difficult problem to work on. The kind of thing hackers love. I bet we are going to see prototype drivers and hardware details much faster than we think.

All of this requires a good knowledge of ARM 64-bit Assembly Language, so consider my book as a great way to learn all the details on how it works. I even have a chapter on reverse engineering which is hopefully helpful.

Written by smist08

January 15, 2021 at 10:59 am

Apple M1 Assembly Language Hello World

with 15 comments

Introduction

Last week, we talked about using a new Apple M1 based Macintosh as a development workstation and how installing Apple’s development system XCode also installed a large number of open source development tools including LLVM and make. This week, we’ll cover how to compile and run a simple command line ARM Assembly Language Hello World program.

Thanks to Alex vonBelow

My book “Programming with 64-Bit ARM Assembly Language” contains lots of sample self contained Assembly Language programs and a number of iOS and Android samples. The command line utilities are compiled for Linux using the GNU tool set. Alex vonBelow took all of these and modified them to work with the LLVM tool chain and to work within Apple’s development environment. He dealt with all the differences between Linux and MacOS/iOS as well. His version of the source code for my book, but modified for Apple M1 is available here:

https://github.com/below/HelloSilicon.

Differences Between MacOS and Linux

Both MacOS and Linux are based on Unix and are more similar than different. However there are a few differences of note:

  • MacOS uses LLVM by default whereas Linux uses GNU GCC. This really just affects the command line arguments in the makefile for the purposes of this article. You can use LLVM on Linux and GCC should be available for Apple M1 shortly.
  • The MacOS linker/loader doesn’t like doing relocations, so you need to use the ADR rather than LDR instruction to load addresses. You could use ADR in Linux and if you do this it will work in both.
  • The Unix API calls are nearly the same, the difference is that Linux redid the function numbers when they went to 64-bit, but MacOS kept the function numbers the same. In the 32-bit world they were the same, but now they are all different.
  • When calling a Linux service the function number goes in X16 rather than X8.
  • Linux installs the various libraries and includes files under /usr/lib and /usr/include, so they are easy to find and use. When you install XCode, it installs SDKs for MacOS, iOS, iPadOS, iWatchOS, etc. with the option of installing lots for versions. The paths to the libs and includes are rather complicated and you need a tool to find them.
  • In MacOS the program must start on a 64-bit boundary, hence the listing has an “.align 2” directive near top.
  • In MacOS you need to link in the System library even if you don’t make a system call from it or you get a linker error. This sample Hello World program uses software interrupts to make the system calls rather than the API in the System library and so shouldn’t need to link to it.
  • In MacOS the default entry point is _main whereas in Linux it is _start. This is changed via a command line argument to the linker.

Hello World Assembly File

Below is the simple Assembly Language program to print out “Hello World” in a terminal window. For all the gory details on these instructions and the architecture of the ARM processor, check out my book.

//
// Assembler program to print "Hello World!"
// to stdout.
//
// X0-X2 - parameters to linux function services
// X16 - linux function number
//
.global _start             // Provide program starting address to linker
.align 2

// Setup the parameters to print hello world
// and then call Linux to do it.

_start: mov X0, #1     // 1 = StdOut
adr X1, helloworld // string to print
mov X2, #13     // length of our string
mov X16, #4     // MacOS write system call
svc 0     // Call linux to output the string

// Setup the parameters to exit the program
// and then call Linux to do it.

mov     X0, #0      // Use 0 return code
       mov     X16, #1     // Service command code 1 terminates this program
       svc     0           // Call MacOS to terminate the program

helloworld:      .ascii  "Hello World!\n"

Makefile

Here is the makefile, the command to assemble the source code is simple, then the command to link is a bit more complicated.

HelloWorld: HelloWorld.o
ld -macosx_version_min 11.0.0 -o HelloWorld HelloWorld.o -lSystem -syslibroot
`xcrun -sdk macosx --show-sdk-path` -e _start -arch arm64

HelloWorld.o: HelloWorld.s
as -o HelloWorld.o HelloWorld.s

The xcrun command is Apple’s command to run or find the various SDKs. Here is a sample of running it:

stephensmith@Stephens-MacBook-Air-2 ~ % xcrun -sdk macosx –show-sdk-path
objc[42012]: Class AMSupportURLConnectionDelegate is implemented in both ?? (0x1edb5b8f0) and ?? (0x122dd02b8). One of the two will be used. Which one is undefined.
objc[42012]: Class AMSupportURLSession is implemented in both ?? (0x1edb5b940) and ?? (0x122dd0308). One of the two will be used. Which one is undefined.
objc[42013]: Class AMSupportURLConnectionDelegate is implemented in both ?? (0x1edb5b8f0) and ?? (0x1141942b8). One of the two will be used. Which one is undefined.
objc[42013]: Class AMSupportURLSession is implemented in both ?? (0x1edb5b940) and ?? (0x114194308). One of the two will be used. Which one is undefined.
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk
stephensmith@Stephens-MacBook-Air-2 ~ %

After the ugly warnings from Objective-C, the path to the MacOS SDK is displayed.

Now we can compile and run our program.

stephensmith@Stephens-MacBook-Air-2 Chapter 1 % make -B
as -o HelloWorld.o HelloWorld.s
objc[42104]: Class AMSupportURLConnectionDelegate is implemented in both ?? (0x1edb5b8f0) and ?? (0x1145342b8). One of the two will be used. Which one is undefined.
objc[42104]: Class AMSupportURLSession is implemented in both ?? (0x1edb5b940) and ?? (0x114534308). One of the two will be used. Which one is undefined.
ld -macosx_version_min 11.0.0 -o HelloWorld HelloWorld.o -lSystem -syslibroot `xcrun -sdk macosx –show-sdk-path` -e _start -arch arm64 
stephensmith@Stephens-MacBook-Air-2 Chapter 1 % ./HelloWorld 
Hello World!
stephensmith@Stephens-MacBook-Air-2 Chapter 1 %

Summary

The new Apple M1 Macintoshes are running ARM processors as part of all that Apple Silicon and you can run standard ARM 64-bit Assembly Language. LLVM is a standard open source development tool which contains an Assembler that is similar to the GNU Assembler. Programming MacOS is similar to Linux since both are based on Unix and if you are familiar with Linux, most of your knowledge is directly applicable.

Written by smist08

January 8, 2021 at 10:31 am

Apple M1 as a Development Workstation

with 12 comments

Introduction

I’ve been playing with my new M1 based Apple MacBook Air for a few weeks now, so I thought I’d blog about how it is as a development machine. These are now the best way to develop iOS Apps for iPhones and iPads. These systems are really new so there are a few missing pieces, but these are filling in fast. You can run most MacOS Intel based programs using Rosetta 2, but I’m interested in what runs in natively compiled ARM code that ideally uses the builtin M1 functionality where appropriate.

XCode

XCode is Apple’s IDE for development. The whole XCode system is a combination of Apple created software along with a number of open source development tools. As long as you don’t compile or debug for Intel, you don’t need Rosetta installed to run all these. That means besides XCode and Swift, you also get natively compiled versions of LLVM, Python and a number of other tools. After installing XCode (which is huge), you can run command line tools to compile C code and Assembly language code. There is a version of make installed and you can do this all from a command shell without using the XCode IDE at all. All these tools are very fast and seem to work perfectly in the native ARM environment. This shouldn’t be too much of a surprise as these have all been working fine on the ARM based Raspberry Pi for quite a few years now.

If you develop iOS or MacOS applications using Cocoa then this is the platform for you. On older Intel based Macs, to test the application the computer had to emulate the ARM processor and the iOS simulators were quite slow and clunky. Now that everything is using the same processor, suddenly the iPhone and iOS simulators are fast and much more productive. In fact currently the M1 processor is faster than any existing iPhone or iPads, so iOS apps actually run fastest on the new Macs.

What’s Missing?

Apple has fully embraced the LLVM open source toolchain and helped have that project fully support the new Macs. Sadly they didn’t provide the same level of help to the GNU GCC open source toolchain. There are now test builds of the GNU toolchain, or you can build it yourself, but not a released one yet. This then slowed down the development of any applications that depend on the GNU toolchain. The most notable case is anything written in Fortran was stalled because GNU has the good Fortran compiler.

Now you might ask, so what? Who uses Fortran these days anyway? The thing is nearly all scientific libraries were written in the 60s and 70s in Fortran and have all been made open source. These libraries are highly reliable and work great. Now you might ask, so what? Not many people do scientific computing? The thing is that all modern machine learning and AI applications make extensive use of these libraries especially for things like linear algebra. This means that even though Python itself is available natively for the Apple M1, all the scientific libraries, most notably numpy are not as they contain Fortran components. Again there are test builds available, but it could be a few months before these are all generally available.

Another problem is the Apple M1 GPU and machine learning accelerator components. Even once these all compile and are available for the M1, which should be soon, it may be considerably longer before versions are available that can make use of the GPU or TPU for vector acceleration. Most of these libraries have support for nVidia and AMD GPUs, however now that Apple has gone their own way, it may be a bit of a wait for Apple versions. Apple has allocated engineers to help with these projects so hopefully it is sooner than later, and any project that previously supported acceleration on an iPhone or iPad will be good to go.

Meanwhile if you use some other programming language or system, like say ERLang, you will have to check their website for native availability, compile them yourself or use Rosetta 2.

The new XCode is great for Apple mobile development, but what about Android? Android Studio is currently being ported to the M1 and there are test builds available, but with lots of missing pieces. Once complete, this will be the best way to develop Android applications since again, you can run the apps natively and don’t require an ARM emulator for testing and debugging.

Summary

Whenever a new generation of hardware is released, there is always a delay as software catches up. If you do a lot of development for iOS then you need one of these new Macs as these are now the best environment for mobile development. Once Android Studio finishes their M1 version, the new Apple M1’s will be by far the best platform for mobile development. Apple has done a really good job of having so much work at release for their new generation of Macintosh computers; but, as is always the case at the bleeding edge, there are a few holes to be filled in. Of course most of these projects are open source so if you need them, you can always contribute to help them move a little faster. As more and more M1 based Macs ship and get into people’s hands these problems will start to be knocked off and more things will move into the “it just works” category.

Written by smist08

January 1, 2021 at 11:54 am

Apple M1 MacBook Air Review

with 4 comments

Introduction

I received my new shiny golden ARM based MacBook Air a few days ago, so I thought I’d write this blog posting on my initial impressions. This replaces our aging 2012 MacBook Air which continues to be a good computer, but sadly it isn’t supported by the new Big Sur MacOS release. It is also a bit limited with only 4Gig RAM and a 128Gig SSD. The new MacBook Air has 16Gig RAM, a 512Gig SSD and has an 8 core GPU.

Same Old Mac

The first impression when you turn it on and perform the initial setup, is that it is the same MacOS as you are used to. Everything works the same and you wouldn’t know there is a new ARM based CPU running behind the scenes. The screen is really nice at a resolution of 2560×1600, the keyboard is the updated magic keyboard and is quite nice to type on. The laptop is relatively lite and the battery lasts a long time.

Installing Software

Using software natively compiled for the new Apple M1 ARM processor is the best and there is already a lot of software available this way. All the Apple software, of course, is compiled for ARM. I installed XCode, which took a little while since it is so large. As long as you don’t compile for Intel, you don’t need the Rosetta emulator to run this. I installed Microsoft Office and Google Chrome, both of which are natively compiled for ARM and run great.

Bigger companies all bought or were provided with the developer prototype hardware to get ready for the real release, however smaller developers and open source developers weren’t going to pay the $600 for the prototypes that you were contractually required to trade in when the real release happened. Now that the ARM based Macs are released and people are receiving them, we are seeing lots of projects with test native ARM builds posted. Further, Apple engineers are contributing to a number of open source projects to help them move a little quicker.

After all this, I needed some utilities that weren’t compiled natively yet, so I let Rosetta install. Then the utilities installed and worked seamlessly. I haven’t installed a great many non-native applications, but for the ones I have installed, I’m impressed that they just work and you can’t see any sign of all the magic working in the background to seamlessly emulate an Intel processor.

You Can Run iOS Apps

Besides running MacOS applications, you can run iOS Apps. From the App Store you can select most iOS Apps to install as well. When I heard about this, I didn’t think I’d really need this. However, it lets you do some things that I couldn’t do before. For instance, a complaint about EchoLink, a ham radio program, is that you need to run it on a phone. Now I can run it on my laptop, which I find handy.

There are actually a number of useful phone or tablet apps that are handy to finally be able to run on a laptop.

UPS Sucks

When I ordered the MacBook, I ordered from apple.ca, so I sort of expected it would ship from Canada. There were no options for shipping, it just said free shipping, so that is what I got. The website said to expect two to three weeks before it ships. OK fine. Then exactly two weeks later, I received the notification that it had shipped. I clicked on the track shipping button and found out it was shipping from Shanghai, China via UPS. Oh no, I’ve never had a good experience with UPS. It took three days to leave Shanghai and then showed up in Incheon, South Korea. Then the next day, tracking showed it in Anchorage, Alaska. What the heck, was it on a boat sailing around the Pacific? The next day, Louisville Kentucky, so it was flying. Sucks that UPS routes everything through this hub. Next day, Seattle Washington. Next day Richmond, BC. It then sat in Richmond for three days. Then it moved to Gibsons, BC and sat there for three days before being delivered. It seems to me that sending anything UPS is about the worst way you can ship things.

Apple sucks that they use UPS. For instance, my experience with FedEx is that it would have flown from Shanghai to Vancouver directly and then gotten here the next day, since FedEx does two deliveries each weekday and then also delivers on Saturdays. FedEx is the best, but Purolator and DHL aren’t far behind. Again UPS sucks, please Apple stop using them.

Summary

The MacBook Air is a very nice laptop. It is well made, light and fast. It’s easy to work on and the long battery life makes it ideal for a mobile workforce. There is tons of software available, naively compiled MacOS, Intel based MacOS and then all the iOS Apps. If you are looking for a new laptop or an upgrade to an old Mac Mini, then these new M1 based Apple Macs are a great choice.

Written by smist08

December 23, 2020 at 11:10 am

Posted in Life, Mobility

Tagged with , , , ,

What’s Next for the Apple M2 ARM CPU

with 3 comments

Introduction

Last week, Apple started shipping their new ARM M1 based Macintosh computers. I ordered a new MacBook Air and maybe I’ll get it before XMas. The demand for these new ARM based computers is high and they are selling like mad. The big attraction is that they have the computing power of top end Intel chips, but use a tenth of the power, leading to a new class of powerful laptops with battery life now measured in days rather than hours. With all the hype around the M1, people are starting to ask where Apple will go next? When will there be an M2 chip and what will it contain? Apple is a secretive company so all this speculation is mostly rumours. This article will look at my wish list and where I think things will go.

First, there will be more M1 base Macs early next year. Expect higher end MacBook Pros, these won’t have a completely new M2, more like an M1X which will have either more CPU or CPU cores and higher memory options. I expect the real M2 will come out towards next XMas as Apple prepares all their new products for the next holiday shopping season.

The Chip Manufacturing Process

The current M1 CPU is manufactured using TSMC’s 5nm process. TSMC recently completed their 3nm fabrication facility (at least the building). The expectation is that the next generation of Apple’s iPhone, iPad and Mac chips will be created using this process. With this size reduction, Apple will be able to fit 1.67 times as many transistors on the chip using the same form factor and power. Compare this to Intel which has been having trouble making the transition from 14nm to 10nm over the last few years. Of course AMD also uses TSMC to manufacture their chips, so there could be competitive AMD chips, but reaching the same power utilization as an ARM CPU is extremely difficult.

Samsung manufactures most of its chips using 8nm technology and is investing heavily trying to catch up to TSMC, hoping to get some of Apple and AMD’s business back. I don’t think Samsung will catch up in 2021 but beyond 2021, the competition could heat up and we’ll see even faster progress.

More Cores

The most obvious place to make use of all these extra transistors is in placing more CPU, GPU or AIPU cores on the chip. The M1 has 8 CPU cores, 8 GPU cores and 16 AI Processor cores. Apple could add to any of these. If they want a more powerful gaming computer, then adding GPU cores is the obvious place. I suspect 8 CPU cores is sufficient for most laptop workloads, but with more GPU cores, they could start being competitive with top of the line nVidia and AMD GPUs. The AI processing cores are interesting and are being used more and more, 

Apple is continually profiling how their processor components are used by applications and will be monitoring which parts of the system are maxed out and which remain mostly idle. Using this information they can allocate more processing cores to the areas that need it most.

More Memory

The current M1 chips come with either 8 or 16 GB of RAM. I suspect this is only a limitation of trying to get some systems shipping in 2020 and that there will be higher memory M1 chips sooner than later. For the M2 chip, I don’t think we really need an 8GB model anymore and if there are two sizes it should be 16 or 32 GB. Further, with high end graphics using a lot of memory, a good case for 64 GB can be made even for a laptop.

More and Faster Ports

The first few Mac computers have 2 USB 4 ports and one video port. There has been a lot of complaining about this, but it is a bit misleading because you can add hubs to these ports. It has been demonstrated that you can actually connect 6 monitors to the video out using a hub. Similarly you can connect a hub and have any number of ports. I’m not sure if Apple will add more ports back and either way I’m not too worried about it.

The good thing is that USB 4 is fast and it makes connecting an external drive (whether SSD or mechanical) more practical for general use. Of course making the ports even faster next time around would be great.

General Optimizations

Each year, ARM improves their CPU cores and Apple incorporates these improvements. The optimizations could be to the pipeline processing, improved algorithms for longer running operations, better out of order execution, security improvements, etc. There are also newer instructions and functionality incorporated. Apple takes all these and adds their own improvements as well. We’ve seen this year over year as the performance of the ARM processors have improved so much in the iPhones and iPads. This will continue and this alone will yield a 30% or so performance improvement.

More Co-processors

The M1 chip is more than a multi-core ARM CPU. It includes all sorts of co-processors like the GPU cores and AI processing. It includes the main RAM, memory controller, a security processor and support for specialty things like video decoding. We don’t know what Apple is working on, but they could easily use some fraction of their transistor budget to add new specialty co-processors. Hopefully whatever they do add is open for programmers to take advantage of and not proprietary and only used by the operating system.

Summary

The Apple M1 Silicon is a significant first milestone. Everyone is excited to see where Apple will go with this. Apple has huge resources to develop these chips going forwards. The R&D Apple puts into Apple Silicon benefits all their businesses from the Apple Watch to the iPad, so they are fully committed to this. I’m excited to see what the next generation chips will be able to do, though I’m hoping to use my M1 based MacBook for 8 years, like I did with my last MacBook.

If you are interested in the M1 ARM processor and want to learn more about how it works internally, then consider my book: Programming with 64-Bit ARM Assembly Language.

Written by smist08

November 27, 2020 at 10:56 am

Apple M1 Unified Memory

leave a comment »

Introduction

I recently upgraded three 2008 MacBook Pros from 1Gig to 4Gig of RAM. It was super-easy, you remove the battery (accessible via a coin), remove a small cover over the RAM and hard-drive, then pop the RAM and push in the new ones. Upgrading the hard drive or RAM on these old laptops is straightforward and anyone can do it. Newer MacBooks require partial disassembly which makes the process harder. For the newest ARM based MacBooks, upgrading is impossible. So, do we gain anything for this lack of upgradeability?

This article looks at Apple’s new unified memory architecture that they claim gives large performance gains. Apple hasn’t released a lot of in depth technical details on the M1 chip, but from what they have released, and now that people have received these units and performed real benchmarks we can see that Apple really does have something here.

Why Would We Want To Upgrade?

In the case of the 2008 MacBook Pro, when it was new, 4Gig was expensive. Now 4Gig of DDR2 memory is $10. It makes total sense to upgrade to maximum memory. Similarly, the MacBook came with a mechanical hard drive which is quite small and slow by modern standards. It was easy to upgrade these to larger faster SSD drives for around $40 each.Often this is the case that the maximum configuration is too expensive at the time of the original purchase, but becomes much cheaper a few years later. Performing these upgrades then lets you get quite a few years more service out of your computer. The 2008 MacBook Pros upgraded to maximum configuration are still quite usable computers (of course you have to run Linux on them, since Apple software no longer supports them).

Enter the New Apple ARM Based Macintoshes

The newly released MacBooks based on ARM System on a Chips (SoCs) have their RAM integrated into their CPU chips. This means that unless you can replace the entire CPU, you can’t upgrade the RAM. Apple claims integrating the memory into the CPU gives them a number of performance gains, since the memory is high speed, very close to all the devices and shared by all the devices. A major bottleneck in modern computer systems is moving data between memory and the CPU or copying data from the CPU’s memory to the GPU’s memory.

AMD and nVidia graphics cards contain their own memory separate from the memory used by the CPU. So a modern gaming computer might have 16Gig RAM for the CPU and then 8Gig or RAM for the GPU. If you want the GPU to perform a matrix multiplication you need to transfer the matrices to the GPU, tell it to multiply them and then transfer the resulting matrix back to the CPU’s memory. nVidia and AMD claim this is necessary since they incorporate newer faster memory in their GPUs than is typically installed on the CPU motherboard. Most CPUs currently use DDR4 memory whereas GPUs typically incorporate faster DDR6 memory. There are GPUs (like the Raspberry Pi’s) that share CPU memory, however these tend to be lower end (cheaper since they don’t have their own memory) and slower since there is more contention for the CPU memory.

The Apple M1 tries to address these problems by incorporating the fastest memory and then providing a much wider bandwidth between the memory and the various processors on the M1 chip. For the M1 there isn’t just the GPU, but also a Neural Engine for AI processing (which is similar to a GPU) as well as other units for specialized functions like data encryption and video decoding. Most newer computers have a 64-bit memory controller that can move 64-bits of data between the CPU and RAM at the speed of the RAM, sometimes the RAM is as fast as the CPU, sometimes it’s a bit slower. Newer CPUs have large caches to try to save on some of this transfer, but the caches are in MegaBytes whereas main memory is in GigaBytes. Separate GPU memory helps by having a completely separate memory controller, expensive servers help by having multiple memory controllers. Apple’s block diagrams seem to indicate they have two 64-bit memory controllers or parallel pathways to main memory, but this is a bit hypothetical. As people are benchmarking these new computers, it does appear that Apple has made some significant performance improvements.

Summary

If Apple has greatly reduced the memory bottleneck and having the GPU, Neural Engine and CPU all accessing the same memory doesn’t cause too much contention, then saving the copying of data between the processing units will be a big advantage. On the downside, you should overbuy on the memory now, since you can’t upgrade it later.

If you are interested in the M1 ARM processor and want to learn more about how it works internally, then consider my book: Programming with 64-Bit ARM Assembly Language.

Written by smist08

November 20, 2020 at 1:09 pm

Apple Mac ARM M1 Competitive Trade-offs

with 3 comments

Introduction

Earlier this week, Apple started their transition from using Intel CPUs to ARM CPUs in their Macintosh laptop and desktop computers. Although the actual computers don’t ship for a week or two, there has been a lot of press coverage comparing these to various Intel/AMD offerings. In this article, we’ll look at what Apple is trying to accomplish with this transition. Note that Apple pushed out three computers to fulfill their promise of shipping ARM based Macs before the end of the year, so there is lots of room for gaps in the current offerings to be filled early in 2021.

It’s All About Power and Heat

iPhones and iPads are powerful computers in their own right, and they need to run for days without recharging. Compare that to top of the line gaming laptops that have trouble running for more than an hour or two without recharging. Apple realizes that we live in a mobile world and don’t like being a slave to power cords. With the ARM processor, Apple realized that they now have the power of Intel/AMD chips, but at a fraction of the power. Hence, the new M1/ARM based Apple Laptops can run for possibly 24 hours of use between charges. This means, if you are ever allowed to fly to Australia again, you can work for the entire 16 hour flight without a worry.

The M1 ARM CPU used in these new computers utilize the ARM big.LITTLE architecture that allows the mixing of high power/low efficiency cores with low power/high efficiency cores on the same chip. The MacOS Big Sur has been tuned to correctly schedule threads to the appropriate type of core for the job that needs to be done. The M1 has four high power cores and four low power cores. Intel is considering introducing big.LITTLE chips, but if they do, they will need to get both Windows and Linux to add support for this, until then, it will do more harm than good.

The M1 ARM CPU is manufactured using TSMC’s 5nm process technology which allows record numbers of transistors on a chip. As a consequence Apple has combined the CPUs, GPUs, memory and other co-processors all onto a single chip. This is a potential problem as the more functions of the chip in use at once, the more heat will be produced. The MacBook Air doesn’t even have a fan, so it will be interesting to see how it manages under heavy load. If you run a workload that utilizes all eight cores, is graphics intensive and performs AI computations, then that will be a lot of transistors in use at once.

All modern chips monitor their temperature and if it gets too high then they throttle down the speed. The higher the clock rate of a CPU, the higher the power consumed and the more heat produced. Thus throttling down the CPU is a good method to let a CPU cool off.

Apple has chosen their design to maximize battery life, which means reducing the power used. Reducing the power used, then reduces the cooling required that then saves on the energy and space used by fans. Apple is making big claims about how good the performance of these new computers will be, while maintaining low power usage and not overheating. If they are successful at this, it will be a huge competitive advantage.

Look at the top performing Intel/AMD gaming systems. They require liquid cooling on the CPU chip and there are typically six fans installed in the case. Then an nVidia RTX3090 has a further three fans installed on the GPU board. With all this, people still complain about overheating and the throttling of the games they are playing. Are the new Apple systems going to turn these into dinosaurs?

Software Compatibility

The downside of switching CPU types, is that you require all your software to be re-compiled. Admittedly for Java, you just need the JavaVM recompiled, and interpreted programs like written in Python, Julia or JavaScript shouldn’t require reworking. The truth is that people have been running Intel programs for a long time and they probably have a number of programs that are written in Objective-C (being the Macs previous favorite programming language) and that these will need to be recompiled to work best on the new Macs.

MacOS Big Sur includes an Intel emulation layer called Rosetta that can efficiently translate Intel Assembly language into ARM Assembly language and the claim is that it works quite well. If you received a new M1 MacBook Air today, it would run Microsoft Office this way. Microsoft has a beta of a native ARM version of Office, but the released version could be a few weeks or months away.

Note that the Rosetta emulation layer can’t run everything. If the program uses newer Intel AVX instructions, then it will fail. The emulation only covers the core instruction set and not any extensions like the AVX vector processing instructions.

Another problem is virtual machines (VMs). Many Mac users occasionally have a need for a Windows program. With Intel based Macs you can run Windows in a VM. With the new M1 Macs, new VM software is going to be required to run Intel VMs on the ARM processors and I don’t know how well this will work.

On the other hand, now that the Macs are ARM based, they can natively run all iPhone and iPad apps, so although you lose a few compiled programs and VMs, you gain all this mobile software.

The Raspberry Pi is also ARM based and has a huge amount of software available for this. The Raspberry Pi has already driven a lot of software to ARM, perhaps helping ease the road for Apple.

Summary

Apple made the choice to switch to the ARM processor for their Mac computers to greatly lower power usage, extending their laptop’s battery life. The cost of switching CPUs is software compatibility, which Apple is mitigating with Rosetta and the availability of iOS apps. I think long term this strategy is a winning one. If you mostly run Apple software like Pages or Final Cut then you can get a new Mac now, if you need it. If you are a developer, developing for iOS using XCode then these M1 Macs become the development platform of choice since you can run your software directly without emulation.

Otherwise, you might want to wait till the software you use is re-released with ARM native versions, also down the road there will be more powerful models, for instance with memory beyond 16GB.

If you are interested in the M1 ARM processor and want to learn more about how it works internally, then consider my book: Programming with 64-Bit ARM Assembly Language.

Written by smist08

November 13, 2020 at 10:51 am

Posted in Business, Mobility

Tagged with , , , ,